• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 251
  • Last Modified:

Javascript for CSS driven menu needs a delay before the items disappear

I am using a compact javascript (written by others and widely available) to build a dynamic menu based mostly on CSS. The script changes the CSS class on unordered list items.  Everything works fine, except that on sub-menus that fly out to the right, I need a slight time delay, so the menu doesn't disappear before the user can get to it.

Here's the base code:

function menuFix() {
      
/*--------------fix horizontal menu--------------------------*/
      var sfEls = document.getElementById("hNav").getElementsByTagName("li");
      for (var i=0; i<sfEls.length; i++) {
            sfEls[i].onmouseover=function() {
            this.className+=(this.className.length>0? " ": "") + "sfhover";
            }
            
            sfEls[i].onmouseout=function() {
            this.className=this.className.replace(new RegExp("( ?|^)sfhover\\b"), "");
            }
            
      }
      
}

I've tried adding a setTimeout in front of the onmouseout function, like: sfEls[i].onmouseout=setTimeout("function()",1000) {

That doesn't work at all.

I've tried adding a setTimeout on the next line, like:
setTimeout('this.className=this.className.replace(new RegExp("( ?|^)sfhover\\b"), "")',1000);

When added here the menus never disappear.

Is it possible to add a delay to this javascript code, or will I have to scrap this approach and use an entire different structure?  Any help will be greatly appreciated.

Teri
0
thoyer
Asked:
thoyer
  • 2
2 Solutions
 
scrathcyboyCommented:
No the problem is simply in the - setTimeout() call.  It is a very conceptually complex call that must in fact call itself.  I struggled with this for a while, until I finally got it right.  Lets see if some links can help you understand the "unobviousness" of this javascript timer loop (it is hard to "get it" at first) --

www.pageresource.com/jscript/jtimeout.htm
http://www.codingforums.com/archive/index.php?t-65386.html
(see in this second one how the timeout is calling a FUNCTION, it is not running the function alone?)
www.the-cool-place.co.uk/javascript/tutorial/javascript2.html
http://www.pageresource.com/jscript/jtimeout.htm

Same with this last one, see how the setTimeout is actually calling the function that you are timing?

Hope that helps.  This is a case where LEARNING this complex function is better than an easy answer!!

0
 
jmanGJHS97Commented:
In case that doesn't work for you, there's several cool menu scripts on dynamicdrive.com.  I happen to like this one:

http://www.dynamicdrive.com/dynamicindex1/jimmenu/index.htm

I totally agree with the idea of figuring out what's going on with your menu.  However, if you need a fresh look, there are tons of examples that contain working code, which may help you think of ways to resolve this current issue are facing.

jmaGJHS97
0
 
thoyerAuthor Commented:
I read through everything in the tutorials you provided. I tried calling the function within the function, like illustrated.  But nothing works.  I think this is more complex than it's worth, and I'm going to redesign so the menu doesn't fly out, but instead drops down.  This will work fine.

But thanks for the effort.
0
 
scrathcyboyCommented:
Which is not a bad idea.  The site where I used a setTimeout() and spent a LOT of time on it, I am thinking of redesigning that site too.  So maybe animated stuff doesnt work as good as other options.  Sometimes you are better to pick what the user expects, it makes it easire for them.   Good luck....
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now