我有類似打字機的效果的代碼,其中段落拼寫出一個字母在一個短的時間延遲。它通過遍歷for循環並將每個字母附加到div來工作。這是一個奇怪的部分:如果切換標籤頁或在運行時儘量減少Chrome瀏覽器,它會混淆並打印亂碼,但如果您停留在正在運行的標籤上,它可以正常工作。在Firefox中,它可以正常工作,當您更改標籤頁時不會出現奇怪現象。到底是怎麼回事?這裏是我的代碼:Chrome和jQuery通過for-loop奇怪的問題多次追加()通過for-loop
$(function() {
var global_tw = "Well, the way they make shows is, they make one show. That show's called a pilot. Then they show that show to the people who make shows, and on the strength of that one show they decide if they're going to make more shows. Some pilots get picked and become television programs. Some don't, become nothing. She starred in one of the ones that became nothing.";
var codeWordsFlag = false;
function typewriter() {
var tw = global_tw.split('');
for (i = 0; i <= tw.length; i++) {
delayPrint(i);
};
function delayPrint(i) {
setTimeout(function(){
$('.codeWords').append(tw[i]);
}, 75*i); //75
};
codeWordsFlag = true;
};
typewriter();
})
我還要提到,我試圖 「追加」 使用文本()和HTML()(例如,$(」碼字 ')文本($(' 字。)。 text()+ tw [i]);),雖然這似乎減少了鉻的錯誤率,錯誤仍然發生在切換標籤/最小化時。
這裏的小提琴(嘗試一下,看看我的意思):http://jsfiddle.net/Shambolaz/Xe56x/14/
感謝您的閱讀。
編輯:檢查邁克爾的答案,解決Chrome最小化時解決延遲超時調用的問題。
Rad,謝謝你。我實現了它,它的工作,感謝您的幫助。 – Ber