简体   繁体   English

setTimeout函数没有调用?

[英]setTimeout function isn't called?

To put a delay on a menu onmouseover effect, setTimeout is one of the options. 要在菜单上具有鼠标悬停效果上的延迟,setTimeout是选项之一。 But when I try it, the function isn't called. 但是当我尝试它时,没有调用该函数。

HTML: HTML:

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

JavaScript: 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 . 您将立即在setTimeout返回的句柄上调用clearTimeout Why is that? 这是为什么? I believe the code will work as expected if you remove that. 我相信,如果删除该代码,它将按预期工作。

You're calling clearTimeout directly afterward. 之后,您直接调用clearTimeout Whatever for? 为了什么? Remove that line, and it will work correctly. 删除该行,它将正常工作。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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