开发者

Change millisec for window.setInterval function

I used window.setInterval function. this function includes 3 arguments :

setInterval(code,millisec,lang)

I used that like this:

var counter = 1;
window.setInterval(function() {}, 1000 * ++counter);

but when first time set timer (second argument), is not changed and that act Like below code:

window.setInterval(function() {}, 1000);

please write correct code开发者_运维知识库 for change timer


Use window.setTimeout instead.

var delay = 1000;
function myTimer() {
    // do whatever

    window.setTimeout(myTimer, delay);
}

window.setTimeout(myTimer, delay);

You can manipulate delay in the body of your function.


Your problem is that javascript first execute '1000 * ++counter' once and then do not update the time interval.
You should try to use a timeout instead: https://developer.mozilla.org/en/DOM/window.setTimeout
And create a new time out with the new value every time your time out function is called.


Sounds like what you're after is not setInterval but rather setTimeout in a loop:

var counter = 1;
for (var i = 1; i <= 3; i++) {
    window.setTimeout(function() {
        alert("#" + counter);
        counter++;
    }, i * 1000);
}

This will execute three different "timers" one after the other.
Live test case: http://jsfiddle.net/86DRd/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜