Last Updated: February 25, 2016
·
21.01K
· bhousman

You can pass arguments into setTimeout and setInterval

Let's say you want to pass 2 variables to the function you call in setTimeout.

Approach 1

var callback = function(a, b){
    console.log(a + b);  // 'foobar'
};

window.setTimeout(function(){
    callback('foo', 'bar');
}, 1000);

Approach 2

window.setTimeout(callback, 1000, 'foo', 'bar');

(Approach 2: IE Support > 9)

1 Response
Add your response

Another approach is to use bind method of functions:

window.setTimeout(callback.bind(null, 'foo', 'bar'), 1000);

And it is working in IE since 9 version and you can grab polyfill at MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind

over 1 year ago ·