0

To put a delay on a menu onmouseover effect, setTimeout is one of the options. But when I try it, the function isn't called.

HTML:

 <li><a href="#" 
 onmouseover="mopendelay('menu_proj')" 
 <li>

JavaScript:

// open hidden layer
function mopen(id)
{ 
 // cancel close timer
 mcancelclosetime();
 // close old layer
 if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';
 // get new layer and show it
 ddmenuitem = document.getElementById(id);
 ddmenuitem.style.visibility = 'visible';
}
// delay menu open on mouseover
function mopendelay(id) 
{
 var delay = setTimeout(function(){
 alert('delay'); // isn't called
 mopen(id);
 }, 200);
 clearTimeout(delay);
}
asked Oct 20, 2011 at 23:57

4 Answers 4

4

You're clearing timeout before the timeout function can execute.

function mopendelay(id) 
{
 var delay = setTimeout(function(){
 mopen(id);
 }, 200);
}
answered Oct 20, 2011 at 23:59
Sign up to request clarification or add additional context in comments.

1 Comment

OK. Thanks for all the answers. If I remove clear time out it works. I must also remove the debug alert statement to get the right response.
2

You're immediately calling clearTimeout on the handle returned by setTimeout. Why is that? I believe the code will work as expected if you remove that.

answered Oct 20, 2011 at 23:59

Comments

0

You're calling clearTimeout directly afterward. Whatever for? Remove that line, and it will work correctly.

answered Oct 21, 2011 at 0:00

Comments

0

Your clear timeout should be outside of the function passed to setTimeout

answered Oct 21, 2011 at 0:00

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.