更改setInterval的间隔 [英] Change interval of setInterval
问题描述
有没有办法在运行时修改setInterval的函数调用间隔,除了删除它(clearInterval)并再次使用不同的值恢复?
Is there a way of modifying the interval of calls of function set with setInterval during runtime, other than removing it (clearInterval) and reinstating again with a different value?
推荐答案
使用setTimeout,另外这是async JS的非阻塞方法:
Use setTimeout instead, additionally this a non-blocking method for async JS:
var interval = 1000;
function callback() {
console.log( 'callback!' );
interval -= 100; // actually this will kill your browser when goes to 0, but shows the idea
setTimeout( callback, interval );
}
setTimeout( callback, interval );
不要使用 setInterval
,如有些情况(很多 setInterval
+长回调,通常比超时长),由于队列大小有限,浏览器会删除一些回调,从不执行即可。只有 setTimeout
才能保证执行。
Don't use setInterval
, as in some cases (lots of setInterval
+ long callbacks, which are usually longer than timeout), due to limited queue size, some callbacks will be dropped by the browser and never executed. Only setTimeout
guarantees execution.
这篇关于更改setInterval的间隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!