Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass intervalID to interval function in javascript?

In javascript when I create an interval, I want to stop the interval from inside the function, but I don't want to reference the ID value from outside like this

var y = setInterval(function(){ clearInterval(y); }, 1000);

What I want is to pass a variable similar to this style

setTimeout(function(data){alert(data);}, 1000, "data");

This works for setInterval too, except I can't really pass the id value that's returned by the setInterval function, because it gets created after calling it.

Right now I'm doing a hack like this:

var r = [];
var y = setInterval(function(r){ if (r.length==1) { clearInterval(r[0]); } }, 1000, r);
r.push(y);

Does anyone know the right way?

Thanks

like image 431
omega Avatar asked Mar 10 '16 15:03

omega


People also ask

What is setInterval () method in JavaScript?

The setInterval() method calls a function or evaluates an expression at specified intervals (in milliseconds). The setInterval() method will continue calling the function until clearInterval() is called, or the window is closed.

How do I stop a JavaScript function from running at an interval?

The JavaScript setInterval () method returns an ID which can be used by the clearInterval () method to stop the interval. If you only need to execute a function one time, use the setTimeout () method.

What is the difference between `code/function` and `interval` in JavaScript?

`CODE/FUNCTION` is the code, action or function we want to fire after the specified delay `INTERVAL` is the delay or interval time we want to wait to fire code. DELAY value is specified in milliseconds which means to wait 3 seconds we should provide 3000 to the setInterval () function.

Can I pass a string to setInterval() function?

Please, don't pass a string to setInterval, JS functions are first-class objects, so they can be passed as arguments, assigned to variables or returned by other functions. In your example, I'd write the following:


1 Answers

You have to use the return value, but that doesn't mean that the variable needs to be accessible to anything else; just encapsulate:

(function() {
    var y = setInterval(function(){ clearInterval(y); }, 1000);
})();

y in the above is only accessible within the outer anonymous function, which only has the interval function and nothing else in it.

You could even give yourself a reusable setInterval wrapper to do it:

function setIntervalWrapper(callback, time) {
    var args = Array.prototype.slice.call(arguments, 1);
    args[0] = setInterval(function() {
        callback.apply(null, args);
    }, time);
}

That will pass the time handle as the first argument, in front of any others you specify. It also has the benefit of supporting those follow-on arguments reliably cross-browser (some older browsers didn't support passing arguments to the callback).

Gratuitous example:

function setIntervalWrapper(callback, time) {
    var args = Array.prototype.slice.call(arguments, 1);
    args[0] = setInterval(function() {
        callback.apply(null, args);
    }, time);
}

var counter = 0;
setIntervalWrapper(function(handle, arg1, arg2) {
    console.log("Interval callback called, handle is " + handle + ", args are '" + arg1 + "' and '" + arg2 + "'");
    if (++counter > 5) {
        console.log("counter > 5, stopping");
        clearInterval(handle);
    }
}, 500, "a", "b");
like image 94
T.J. Crowder Avatar answered Oct 04 '22 10:10

T.J. Crowder