How to pass a variable to setInterval?

坚强是说给别人听的谎言 提交于 2019-12-01 13:29:34

问题


I need to update the time in my setInterval. The value is returned by the function that setInterval is executing.

read_log(); returns an integer.

var loop = setInterval(function(){count = read_log();}, count);

This returns that count is undefiend. So I need to get the count and pass it to setInterval


回答1:


If you need to change the repetition interval after each call to read_log(), you can't use setInterval() -- that uses a constent repetition. You need to use setTimeout, so you can change the period each time:

function call_read_log() {
    var count = read_log();
    setTimeout(call_read_log, count);
}
call_read_log();


来源:https://stackoverflow.com/questions/25734518/how-to-pass-a-variable-to-setinterval

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!