var interval = 250
setInterval(function(){
if ( 1==1 ) {
interval = 5000
}
}, interval)
This should set interval at 5000 if 1 == 1 but it keeps the interval at 250.
Because interval
in this case is evaluated just once, when setInterval
function is called (note the difference with setInterval
parameter function, which will be called repeatedly).
To make the interval change after the first run, you can use setTimeout
instead, with something like:
setTimeout(function() {
(function _t() {
// doSomethingUseful();
setTimeout(_t, 5000);
})();
}, 250);
You can't change the interval without calling setInterval all over again. I would use some sort of setTimeout loop instead.
执行回调函数时,使用250作为参数调用setInterval,并且在函数中更改间隔值时,此更改不会影响先前的设置值。
setInterval将每250 MS调用一次回调,但是setInterval本身将被调用一次
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.