필자는 JavaScript로 타이포그래피 효과를주는 스크립트입니다. 루프를 실행하려고 했으므로 실행이 끝난 후 10 초가 지나면 루프에서 실행됩니다. 즉, 타이핑 효과가 있고 10 초 후에 텍스트가 쓰여지면 다시 한 번 실행됩니다. 나는 setTimeout()
으로 시도했지만 작동하지 않았습니다.시간 간격에 따른 자바 스크립트 루핑
미리 도움을 주셔서 감사합니다.
function setupTypewriter(t) {
var HTML = t.innerHTML;
t.innerHTML = "";
var cursorPosition = 0,
tag = "",
writingTag = false,
tagOpen = false,
typeSpeed = 50,
tempTypeSpeed = 0;
var type = function() {
if (writingTag === true) {
tag += HTML[cursorPosition];
}
if (HTML[cursorPosition] === "<") {
tempTypeSpeed = 0;
if (tagOpen) {
tagOpen = false;
writingTag = true;
} else {
tag = "";
tagOpen = true;
writingTag = true;
tag += HTML[cursorPosition];
}
}
if (!writingTag && tagOpen) {
tag.innerHTML += HTML[cursorPosition];
}
if (!writingTag && !tagOpen) {
if (HTML[cursorPosition] === " ") {
tempTypeSpeed = 0;
}
else {
tempTypeSpeed = (Math.random() * typeSpeed) + 50;
}
t.innerHTML += HTML[cursorPosition];
}
if (writingTag === true && HTML[cursorPosition] === ">") {
tempTypeSpeed = (Math.random() * typeSpeed) + 50;
writingTag = false;
if (tagOpen) {
var newSpan = document.createElement("span");
t.appendChild(newSpan);
newSpan.innerHTML = tag;
tag = newSpan.firstChild;
}
}
cursorPosition += 1;
if (cursorPosition < HTML.length - 1) {
setTimeout(type, tempTypeSpeed);
}
};
return {
type: type
};
}
var typer = document.getElementById('typewriter');
typewriter = setupTypewriter(typewriter);
typewriter.type();
감사합니다. 나는 그것을 보았다, 그러나 나는 나의 기능으로 이것을 어떻게 사용하는지 모르겠다. JS는 나를 위해 새로운 liitle입니다 (저는 C#을 사용합니다). 아래쪽에서 볼 수 있듯이 3 줄의 코드가 있으며 타이핑 효과가 없으면 작동하지 않습니다. 내가 그 var myVar 같은 코드를 사용할 때; function myFunction() { myVar = setInterval (setupTypewriter, 3000); }이 함수는 onces를 실행합니다 (효과가 표시된 후에 만). 더 이상은 일어나지 않습니다. – Mikisz