setTimeout() and setting parameters
While the marked correct answer is one method of achieving this... I don't believe it is the correct.
See the attached JS Fiddle:http://jsfiddle.net/PWHw3/
What we are doing here basically is:
setTimeout(function, timeout, param);
Example:
var test = function(a){ var b = (a) ? a : "fail"; alert(b);};setTimeout(test, 500, "works");
This works for me, and eliminates the needs to pass through two functions.
To make it work, and do it without using the nasty eval version of setTimeout change:
var t = setTimeout("HideMenu(someNum)", 200);
to this:
var t = setTimeout(function(s) { return function() { HideMenu(s) } }(someNum), 200);
This way you pass the value of someNum
into the variable s
in the scope of the setTimeout
.
setTimeout
accepts a function and a millisecond delay. The function can be either a function reference or a string that will get evaluated when the timeout fires. Your current sequence looks like this:
mouseleave function
- assign variable
someNum
a value. someNum is scoped tothe current function - set a timer to evaluate the string
"HideNum(someNum)"
after 200ms. - end function, leave function scope
- assign variable
200ms passes
"HideNum(someNum)"
is evaluated. It should throw an exception as the variable someNum is undefined. That's why HideNum is not being called - check for errors in your console.
What you want is a function reference, which will keep your someNum in scope (via closure - which you might want to read up on).
setTimeout(function() { HideNum(someNum); }, 200);
You'll find the Mozilla docs a better reference for JavaScript. Here's the window.setTimeout docs.