admin管理员组文章数量:1355093
Granted the following piece of code:
function updateOdometers(odometers) {
setTimeout(function(){
odometers[1].update(odometers[1].value + 10);
}, 500);
}
setInterval(updateOdometers(odometers), 2000);
For whatever reason, this code updates the value of odometer only once, rather than every 2000ms with a delay inside. Googling/SO-ing around didn't get me much of a result. Any ideas?
Granted the following piece of code:
function updateOdometers(odometers) {
setTimeout(function(){
odometers[1].update(odometers[1].value + 10);
}, 500);
}
setInterval(updateOdometers(odometers), 2000);
For whatever reason, this code updates the value of odometer only once, rather than every 2000ms with a delay inside. Googling/SO-ing around didn't get me much of a result. Any ideas?
Share Improve this question asked Mar 3, 2015 at 20:36 favorettifavoretti 30.2k4 gold badges51 silver badges62 bronze badges 4- Why are you trying to do this? – Bergi Commented Mar 3, 2015 at 20:39
- 2 Duplicate of stackoverflow./questions/28837247/… – Telokis Commented Mar 3, 2015 at 20:40
- Blah, didn't find the original question you're referring to. – favoretti Commented Mar 3, 2015 at 20:55
- @Bergi: this is a not very sane sanitized piece of code. I have a number of odometers I need to update. The update has to happen every 6 seconds and inner odometers need to update one by one. Makes sense? – favoretti Commented Mar 3, 2015 at 21:03
1 Answer
Reset to default 7This line :
setInterval(updateOdometers(odometers), 2000);
should be
setInterval(function () {updateOdometers(odometers);}, 2000);
Otherwise you will be calling updateOdometers(odometers)
and passing its result to setInterval
.
本文标签: javascriptsetTimeout() inside setInterval() sequence fires off only onceStack Overflow
版权声明:本文标题:javascript - setTimeout() inside setInterval() sequence fires off only once - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744027048a2578227.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论