存储setInterval的值 [英] storing the value of setInterval
本文介绍了存储setInterval的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我有这样的代码
count=0
count2=setInterval('count++',1000)
count2变量总是设置为2而不是count的实际值,因为它每秒增加
the count2 variable would always set as 2 not the actual value of count as it increases every second
我的问题是:你甚至可以存储seInterval()方法的值
my question is: can you even store the value of the seInterval() method
推荐答案
setInterval()的返回值是一个ID号,可以传递给clearInterval()以阻止定期执行的函数再次运行。这是一个例子:
The return value of setInterval() is an ID number that can be passed to clearInterval() to stop the periodically executed function from running another time. Here's an example of that:
var id = setInterval(function() {
// Periodically check to see if the element is there
if(document.getElementById('foo')) {
clearInterval(id);
weAreReady();
}
}, 100);
在你的例子中,如果你想要 count2
要获得与count相同的值,您可以使用:
In your example, if you want count2
to have the same value as count, you could use:
var count = 0, count2 = 0;
setInterval(function() {
// I wrote this on two lines for clarity.
++count;
count2 = count;
}, 1000);
这篇关于存储setInterval的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文