9

Here is an example situation.

var count,
    time = 1000;

setInterval(function(){
    count += 1;
}, time);

The code above will add 1 to the "count" var, very 1000 milliseconds. It seems that setInterval, when triggered, will use the time it sees on execution. If that value is later updated it will not take this into account and will continue to fire with the initial time that was set.

How can I dynamically change the time for this Method?

Panomosh
  • 884
  • 2
  • 8
  • 18

4 Answers4

16

Use setTimeout instead with a callback and a variable instead of number.

function timeout() {
    setTimeout(function () {
        count += 1;
        console.log(count);
        timeout();
    }, time);
};
timeout();

Demo here

Shorter version would be:

function periodicall() {
    count++;
    setTimeout(periodicall, time);
};
periodicall();
Sergio
  • 28,539
  • 11
  • 85
  • 132
4

Try:

var count,
    time = 1000,
    intId;
function invoke(){

    intId = setInterval(function(){
        count += 1;
        if(...) // now i need to change my time
        {
           time = 2000; //some new value
           intId = window.clearInterval(intId);
           invoke();
        }
    }, time);

}

invoke();

You cannot change the interval dynamically because it is set once and then you dont rerun the setInterval code again. So what you can do it to clear the interval and again set it to run. You can also use setTimeout with similar logic, but using setTimeout you need to register a timeout everytime and you don't need to possibly use clearTimeout unless you want to abort in between. If you are changing time everytime then setTimeout makes more sense.

var count,
time = 1000;

function invoke() {
    count += 1;
    time += 1000; //some new value
    console.log('displ');
    window.setTimeout(invoke, time);
}
window.setTimeout(invoke, time);
PSL
  • 123,204
  • 21
  • 253
  • 243
3

You cant (as far as i know) change the interval dynamically. I would suggesst to do this with callbacks:

var _time = 1000,
_out,
_count = 0,
yourfunc = function() {
    count++;
    if (count > 10) {
        // stop
        clearTimeout(_out); // optional
    }
    else {
        // your code
        _time = 1000 + count; // for instance
        _out = setTimeout(function() {
            yourfunc();
        }, _time);
    }
};
Alex
  • 9,911
  • 5
  • 33
  • 52
1

integers are not passed by reference in JavaScript meaning there is no way to change the interval by changing your variable.

Simply cancel the setInterval and restart it again with the new time.

Example can be found here: http://jsfiddle.net/Elak/yUxmw/2/

var Interval;

(function () {
    var createInterval = function (callback, time) {
        return setInterval(callback, time);
    }

    Interval = function (callback, time) {
        this.callback = callback;
        this.interval = createInterval(callback, time);
    };

    Interval.prototype.updateTimer = function (time) {
        clearInterval(this.interval);
        createInterval(this.callback, time);
    };

})();

$(document).ready(function () {
    var inter = new Interval(function () {
        $("#out").append("<li>" + new Date().toString() + "</li>");
    }, 1000);

    setTimeout(function () {
        inter.updateTimer(500);
    }, 2000);
});
MichaC
  • 13,104
  • 2
  • 44
  • 56