Global variable is logged as undefined when passed as parameter to setTimeout callback function

Alternatively you can do it without creating a closure.

function myFunction(str1, str2) {
  alert(str1); //hello
  alert(str2); //world
}

window.setTimeout(myFunction, 10, 'hello', 'world');

But note it doesn’t work on IE < 10 according to MDN.

Leave a Comment