javascript 질문드려요~
본문
<style>
/* ----------------------------------------
* animation
* ----------------------------------------
*/
@keyframes cursor{
50%{ border-color: transparent; }
}
.text{
display: table-cell;
vertical-align: middle;
font-size: 32px;
height: 32px;
border-right: .05em solid black;
animation: cursor 0.5s ease;
}
</style>
<div class="ani01">
<h1 class="text"></h1>
</div>
<script>
const content = "Typing Effect"
const text = document.querySelector(".text")
let index = 0;
function typing(){
text.textContent += content[index++]
if(index > content.length){
text.textContent = ""
index = 0;
}
}
setInterval(typing, 200)
</script>
타이핑효과를 주는 text animation 인데
이게 계속 반복됩니다.
한번만 보여주고 끝내려면 어딜 고쳐야하나요.... ㅜㅜ
답변 2
<script>
const content = "Typing Effect";
const text = document.querySelector(".text");
let index = 0;
let intervalId;
function typing() {
text.textContent += content[index++];
if (index >= content.length) {
clearInterval(intervalId);
}
}
intervalId = setInterval(typing, 200);
</script>
setInterval(typing, 200) -> setTimeout(typing, 200)