Top
window.setTimeout call functions with parameters / DOM vs MSDN vs MDN – Dragos Cirjan
fade
395
post-template-default,single,single-post,postid-395,single-format-standard,eltd-core-1.1.1,flow child-child-ver-1.0.0,flow-ver-1.3.6,,eltd-smooth-page-transitions,ajax,eltd-blog-installed,page-template-blog-standard,eltd-header-standard,eltd-fixed-on-scroll,eltd-default-mobile-header,eltd-sticky-up-mobile-header,eltd-dropdown-default,wpb-js-composer js-comp-ver-5.0.1,vc_responsive

Warning: A non-numeric value encountered in /usr/src/wordpress/wp-content/themes/flow/framework/modules/title/title-functions.php on line 417

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