嘗試:
var count,
time = 1000,
intId;
function invoke(){
intId = setInterval(function(){
count += 1;
if(...) // now i need to change my time
{
time = 2000; //some new value
intId = window.clearInterval(intId);
invoke();
}
}, time);
}
invoke();
,因爲它被設置一次,你不能動態地更改間隔,然後你不重新運行的setInterval代碼。所以你可以做什麼來清除間隔,並再次設置它運行。您也可以使用類似邏輯的setTimeout
,但使用setTimeout您需要每次註冊一個超時,並且您不需要使用clearTimeout
,除非您想在兩者之間中止。如果你每次都在改變時間,setTimeout更有意義。
var count,
time = 1000;
function invoke() {
count += 1;
time += 1000; //some new value
console.log('displ');
window.setTimeout(invoke, time);
}
window.setTimeout(invoke, time);
來源
2013-09-23 15:43:02
PSL
http://stackoverflow.com/questions/1280263/changing-the-interval-of-setinterval-而它的運行 – Tomalla