views:

40

answers:

2

I have a navigation menu which contain sub menus. On hover I want the sub menus to show after a second delay. Menu items marked with a class of "more" contain sub menus.

Problem is that one of my functions called hoverCheck() is coming back as undefined when it's called. But I can't figure out why.

Here's my code:

$(document).ready(function() {
 navigation();
});

function navigation() {
 var moreMenu = $('.nav li.more');
 var hovering;

 function hoverCheck() {
  hovering = 'hover';
  openMenu();
 }

 function openMenu() {
  if(hovering == 'hover') {
   $(this).children('ul').slideDown('fast');
  } 
 }

 moreMenu.mouseenter(function() {
  setTimeout("hoverCheck()",1000);
 });
 moreMenu.mouseleave(function() {
  hovering = null;
  $(this).children('ul').slideUp('fast');
 });

}
+4  A: 

You're getting an error because that function isn't defined in the global scope, instead you should do this:

setTimeout(hoverCheck, 1000);

As a general rule, try to always avoid passing a string to setTimeout(), give it a function reference directly - otherwise you may run into scoping issues, just like this :)

Nick Craver
That worked (at least for getting the hoverCheck function to run. Seems i have more issues, as the menu is not sliding out, but I confirmed with an alert() that it is entering the hoverCheck function.I can never tell when to call a function with the parentheses and when not to. What's the secret?
Ben
@Ben - first, try to never get in a situation where you're passing a string, then the rules are simple: parenthesis if you want to call the function *right then*, without them you're passing the function itself, rather than the *result* (which calling it will get you) of the function.
Nick Craver
@Nick - Slightly clearer, but very muddy in my mind still. Do you know of a good online resource that explains this in detail? BTW, I got it working now, the problem was I had a $(this) which made no sense in the external function. I had to add a temporary class to the hovered list item and reference it that way.
Ben
A: 

The setTimeout function takes an expression as the first argument, not the name of a function. If you replace

setTimeout("hoverCheck()",1000);

with

setTimeout(function() {hoverCheck();}, 1000);

that might do the trick.

Cameron Skinner
There's no need for an anonymous function here, see my answer for a much more concise (and more efficient) way to do this.
Nick Craver
Aw, too slow. I guess you don't need the "function" wrapper, either.
Cameron Skinner
Man. WAY too slow! As I commented @Nick corrected me!
Cameron Skinner
And it's not strictly true, anyway, since setTimeout can use a string. It's just not often the safest way to accomplish the goal.
Chris Farmer