I am facing a strange issue while using javascript setTimeout function in IE8. I want to use the 'setTimeout' function like this -
setTimeout(timeout,2000, {name:'saarthak'});
function timeout(opts)
{
alert('hello ' + opts.name);
}
the third parameter of the setTimeout is the argument that I want to pass to the calling function. This is working perfect fine in FF, Chrome but not in IE8.
Does anybody have any clue what could be done? Or any work around of achieving this?
Thanks
I am facing a strange issue while using javascript setTimeout function in IE8. I want to use the 'setTimeout' function like this -
setTimeout(timeout,2000, {name:'saarthak'});
function timeout(opts)
{
alert('hello ' + opts.name);
}
the third parameter of the setTimeout is the argument that I want to pass to the calling function. This is working perfect fine in FF, Chrome but not in IE8.
Does anybody have any clue what could be done? Or any work around of achieving this?
Thanks
Share Improve this question asked Aug 10, 2011 at 7:38 saarthaksaarthak 1,0142 gold badges12 silver badges27 bronze badges 1- 1 You could've been finding it yourself: Note that passing additional parameters to the function in the first syntax does not work in Internet Explorer. @developer.mozilla/en/window.setTimeout – KooiInc Commented Aug 10, 2011 at 8:15
2 Answers
Reset to default 11Probably not supported there, so have this instead:
window.setTimeout(function() {
timeout({name:'saarthak'});
},2000);
Meaning call your function from within anonymous function.
If you want to call timeout with changing variable (e.g. calling timeout in loop with lot of names) you can use also in IE8:
var names = ["saarthak", "saarthak2", "saarthak3"];
for (var q in names) {
setTimeout(
(function(opts){
return function(){
alert ("hello " + opts.name)
}
})({name:names[q]}), 2000);
}
see: http://jsfiddle/q4HYz/