Question

To avoid implied eval and unnecessary code I would like to use:
setTimeout(alert, 1000, "Hello");

instead of:
setTimeout('alert("Hello")', 1000);
or:

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

Now I have read that IE supports this from IE8 and up, if I run it from the emulator (in IE11) though, it seems that even IE9 doesn't support it.

Can anyone tell me if this is true for the actual versions of IE8 and IE9?

Was it helpful?

Solution

Fails for me in both IE9 and IE8: http://jsbin.com/nuranote/1 Works for me in IE10 and IE11.

Interestingly, they don't mention this support in the IE Dev Center page for setTimeout...

Now, if alert was just an example but you're not going to be doing this with host-provided functions (which aren't always backed by Function.prototype), you can do this:

// Won't work with `alert` on IE9 but does with real JavaScript functions.
// Even `alert` works IE10+ (they finally got it about Function.prototype)
setTimeout(yourFunction.bind(null, "Hello"), 1000);

That still creates a function, but in a (theoretically) slightly more optimized way. (You have to shim that for IE8 and earlier, possibly a couple of other older browsers.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top