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
user823527
3,72217 gold badges70 silver badges111 bronze badges
4 Answers 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
canon
41.8k10 gold badges77 silver badges102 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
user823527
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.
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
Dan Tao
129k57 gold badges309 silver badges451 bronze badges
Comments
You're calling clearTimeout directly afterward. Whatever for? Remove that line, and it will work correctly.
answered Oct 21, 2011 at 0:00
Comments
Your clear timeout should be outside of the function passed to setTimeout
answered Oct 21, 2011 at 0:00
Ruan Mendes
92.7k31 gold badges162 silver badges225 bronze badges
Comments
lang-js