kxswnw
Last Updated: February 25, 2016
·
2.562K
· bhousman
9b51c0db928eefd139d71297e5893a2e

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)

Say Thanks
Respond

1 Response
Add your response

10708
298941b7505eec5e45bea403f71faefe

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 ·