1 回答

TA貢獻1846條經驗 獲得超7個贊
由于計數設置為1一旦達到字符串末尾的長度,因此您可以添加條件并增加超時(如果滿足):
/* SOLUTION : wait two seconds when new line */
if (typeString[0][i - 1] == '\n') {
setTimeout(type, 1000);
} else if (count === 1) {
setTimeout(type, 3000);
} else {
setTimeout(type, 100);
}
使用<br />'s 時,瀏覽器不會將其注冊為有效的 HTML,直到標記完成。因此,有一秒鐘,所有渲染的內容都是<在標簽的其余部分完成并且它理解該標簽是什么之前。
for (let i = 0; i < 10; i++) {
task(i);
}
function task(i) {
setTimeout(function() {
// Add tasks to do
var typeString = ['? I m Mr.Frits.\n? and I love Pakistan...:)'];
var i = 0;
var count = 0;
var selectedText = '';
var text = '';
var typing = document.getElementById('typing');
(function type() {
if (count == typeString.length) {
count = 0;
};
selectedText = typeString[count];
text = selectedText.slice(0, ++i);
typing.innerHTML = text.fontsize(6);
typing.style.fontFamily = "monospace";
typing.style.color = "black";
typing.style.fontWeight = "normal";
if (text.length === selectedText.length) {
count++;
i = 0;
}
/* SOLUTION : wait two seconds when new line */
if (typeString[0][i - 1] == '\n') {
setTimeout(type, 1000);
} else if (count === 1) {
setTimeout(type, 3000);
} else {
setTimeout(type, 100);
}
}());
}, 1000);
}
<pre id="typing"></pre>
添加回答
舉報