首頁 >web前端 >js教程 >javascript (用setTimeout而非setInterval)_javascript技巧

javascript (用setTimeout而非setInterval)_javascript技巧

WBOY
WBOY原創
2016-05-16 17:57:59944瀏覽
复制代码 代码如下:

var num = 0;
var max = 10;
function incrementNumber() {
num++;
//if the max has not been reached, set another timeout
if (num setTimeout(incrementNumber, 500);
} else {
alert(“Done”);
}
}
setTimeout(incrementNumber, 500);

如果用setInterval 可能出现 下次调用会在前一次调用前调用 (

True intervals are rarely used in production environments because it ' s possible that one interval will begin before the previous one has finished executing)
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn