SetInterval (function (), time) change time at run time

I want to change the time of the setInterval function when my code is running.

I try this

<script type="text/javascript">
        $(function () {
            var timer;
            function come() { alert("here"); }
            timer = setInterval(come, 0);
            clearInterval(timer);
            timer = setInterval(come, 10000);
        });
    </script>

The first SetInterval does not work!

+3
source share
5 answers

You clear the interval on the next line, so the first one will not work, since it clears immediately:

        timer = setInterval(come, 0);
        clearInterval(timer);
        timer = setInterval(come, 10000);

Also, as gdoron says, setting the interval is not valid, and it is not a good idea to either use setTimeout instead, or just run the function if there is no delay.

        come();
        clearInterval(timer);
        timer = setInterval(come, 10000);
+11
source

You can not. You will need to use setTimeout and call it again:

var timer; // current timeout id to clear
function come(){ /* do something */};
var time; // dynamic interval

(function repeat() {
    come();
    timer = setTimeout(repeat, time);
})();

"", , repeat. , , alter time , .

+3

, . , , . :

timer = {
    timers:{},
    inc:0,
    start:function(cb,gap) {
        var key = inc;
        inc++;
        timer.timers[key] = [setInterval(cb,gap),cb];
        return key;
    },
    stop:function(id) {
        if( !timer.timers[id]) return;
        clearInterval(timer.timers[id][0]);
        delete timer.timers[id];
    },
    change:function(id,newgap) {
        if( !timer.timers[id]) return;
        clearInterval(timer.timers[id][0]);
        setInterval(timer.timers[id][1],newgap);
    }
};

:

var myTimer = timer.start(function() {....},1000);
// calls every second

timer.change(myTimer,5000);
// now calls every five seconds
+2
timer = setInterval(come, 0); // zero isn't a valid interval...

, :

come();
timer = setInterval(come, 10000);

docs MDN:

delay - ( ), setInterval() func. setTimeout, .

:

setTimeout() "": setTimeout() , , " ", . , DOM_MIN_TIMEOUT_VALUE, 4 ( Firefox: dom.min_timeout_value) DOM_CLAMP_TIMEOUT_NESTING_LEVEL 5 .

+1

, , - , , - .

setInterval, (Niet the Dark Absol).

function timer()
{
    var timer = {
        running: false,
        iv: 5000,
        timeout: false,
        cb : function(){},
        start : function(cb,iv,sd){
            var elm = this;
            clearInterval(this.timeout);
            this.running = true;
            if(cb) this.cb = cb;
            if(iv) this.iv = iv;
            if(sd) elm.execute(elm);
            this.timeout = setTimeout(function(){elm.execute(elm)}, this.iv);
        },
        execute : function(e){
            if(!e.running) return false;
            e.cb();
            e.start();
        },
        stop : function(){
            this.running = false;
        },
        set_interval : function(iv){
            clearInterval(this.timeout);
            this.start(false, iv);
        }
    };
    return timer;
}

:

var timer_1 = new timer();
timer_1.start(function(){
    //magic here
}, 2000, false);

var timer_2 = new timer();
timer_2.start(function(){
    //more magic here
}, 3000, true);

//change the interval
timer_2.set_interval(4000);

//stop the timer
timer_1.stop();

The last parameter of the start function is boolean if the function should be started at 0.

You can also find the script here: https://github.com/Atticweb/smart-interval

0
source

All Articles