简体   繁体   中英

setTimeout function isn't called?

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);
}

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

function mopendelay(id) 
{
    var delay = setTimeout(function(){
        mopen(id);
    }, 200);
}

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.

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

您的清除超时应该在传递给setTimeout的函数之外

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM