javascript 在 setInterval / setTimeout 中使用变量作为时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18963377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use Variable as Time in setInterval / setTimeout
提问by Panomosh
Here is an example situation.
这是一个示例情况。
var count,
time = 1000;
setInterval(function(){
count += 1;
}, time);
The code above will add 1 to the "count" var, very 1000 milliseconds. It seems that setInterval, when triggered, will use the time it sees on execution. If that value is later updated it will not take this into account and will continue to fire with the initial time that was set.
上面的代码将“计数”变量加 1,非常 1000 毫秒。似乎 setInterval 在触发时将使用它在执行时看到的时间。如果该值稍后更新,则不会考虑这一点,并将继续以设置的初始时间触发。
How can I dynamically change the time for this Method?
如何动态更改此方法的时间?
回答by Sergio
Use setTimeout instead with a callback and a variable instead of number.
使用 setTimeout 代替回调和变量而不是数字。
function timeout() {
setTimeout(function () {
count += 1;
console.log(count);
timeout();
}, time);
};
timeout();
Demo here
演示在这里
Shorter version would be:
较短的版本是:
function periodicall() {
count++;
setTimeout(periodicall, time);
};
periodicall();
回答by PSL
Try:
尝试:
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();
You cannot change the interval dynamically because it is set once and then you dont rerun the setInterval code again. So what you can do it to clear the interval and again set it to run. You can also use setTimeout
with similar logic, but using setTimeout you need to register a timeout everytime and you don't need to possibly use clearTimeout
unless you want to abort in between. If you are changing time everytime then setTimeout makes more sense.
您不能动态更改间隔,因为它设置了一次,然后您不会再次重新运行 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);
回答by Alex
You cant (as far as i know) change the interval dynamically. I would suggesst to do this with callbacks:
你不能(据我所知)动态改变间隔。我建议通过回调来做到这一点:
var _time = 1000,
_out,
_count = 0,
yourfunc = function() {
count++;
if (count > 10) {
// stop
clearTimeout(_out); // optional
}
else {
// your code
_time = 1000 + count; // for instance
_out = setTimeout(function() {
yourfunc();
}, _time);
}
};
回答by MichaC
integers are not passed by reference in JavaScript meaning there is no way to change the interval by changing your variable.
JavaScript 中的整数不是通过引用传递的,这意味着无法通过更改变量来更改间隔。
Simply cancel the setInterval and restart it again with the new time.
只需取消 setInterval 并使用新时间重新启动即可。
Example can be found here: http://jsfiddle.net/Elak/yUxmw/2/
示例可以在这里找到:http: //jsfiddle.net/Elak/yUxmw/2/
var Interval;
(function () {
var createInterval = function (callback, time) {
return setInterval(callback, time);
}
Interval = function (callback, time) {
this.callback = callback;
this.interval = createInterval(callback, time);
};
Interval.prototype.updateTimer = function (time) {
clearInterval(this.interval);
createInterval(this.callback, time);
};
})();
$(document).ready(function () {
var inter = new Interval(function () {
$("#out").append("<li>" + new Date().toString() + "</li>");
}, 1000);
setTimeout(function () {
inter.updateTimer(500);
}, 2000);
});