Solved

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

Posted on 2006-07-10
6
243 Views
Last Modified: 2013-11-19
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
Comment
Question by:thoyer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
6 Comments
 
LVL 44

Accepted Solution

by:
scrathcyboy earned 250 total points
ID: 17104161
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
 
LVL 9

Assisted Solution

by:jmanGJHS97
jmanGJHS97 earned 250 total points
ID: 17104911
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
 

Author Comment

by:thoyer
ID: 17112422
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
 
LVL 44

Expert Comment

by:scrathcyboy
ID: 17112703
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Where does legacy ASP.NET initialize language? 2 56
ASP sending two datas 2 22
JQuery conflicts/syntax issue 4 23
How do I add counter to html listbox using jquery 2 24
Find out what you should include to make the best professional email signature for your organization.
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question