Top

window.setTimeout call functions with parameters / DOM vs MSDN vs MDN

window.setTimeout call functions with parameters / DOM vs MSDN vs MDN

One of my latest WTFs came from a javascript diference between the MSDN (yes… Microsoft Developer Network) and the rest of the world which may actually follow the latest DOM description.

Even though, w3schools.com still presents this as MSDN does, I’m pretty sure the DOM evolved since. So let’s see:

MSDN and w3schools present window.setTimeout as:

var timeoutID = window.setTimeout(code,millisec,lang)

while in MDN you will find another version of calling this function:

var timeoutID = window.setTimeout(func, delay, [param1, param2, ...]);
var timeoutID = window.setTimeout(code, delay);

Calling a function through window.setTimeout is available since Javascript v-1.2, which from my vague knowledge was supported even by Internet Explorer 6.0, what I don’t really understand is why this is done so freaking ugly that you can’t set a timeout for a function call while mentioning its parameters as well.

While every browser based on gecko or webkit will always recognize the MDN calling version, Internet Explorer will not. The only way of making this available in IE is by instantiating the function each time you will actually call it. i.e.

var timeoutID = window.setTimeout((function([param1, param2, ...]){
    return function(){ /* actual use of the param1, param2, etc */ }
})([value1, value2, ...]), delay);

I know. This kinda sucks, but this is the only way I could find to make things work. Now… I let your imagination fly in how to use this piece of crap. Have fun!

Cirjan Dragos
No Comments

Post a Comment