setTimeout 和clearTimeout 分配给 var 并指定

Eas*_*yBB 1 javascript jquery settimeout

var nothover = function(){window.setTimeout(function() {
     $('#proFBfeel').hide();
      $('#proFBfeel .moreinfos').html('');
       $('.contact_pro').html('');
     },3000);
   };
  $('#proFBfeel').nothover();
    $('#proFBfeel').hover(function() {
        window.clearTimeOut(nothover);
     });
         $('html').click(function() {
           $('#proFBfeel').hide();
            $('#proFBfeel .moreinfos').html('');
             $('.contact_pro').html('');
          });
Run Code Online (Sandbox Code Playgroud)

好的,正如您在我分配的内容中看到的,varnothover是 setTimeout

三秒后,我希望该函数运行,除非该对象悬停。如果悬停则清除超时。

然后,一旦回到对象外部,再次运行该函数,或者除非他们单击 HTML 元素,然后隐藏该对象。

尽管它对我说,但它运行不正常

未捕获的类型错误:对象 [object Object] 没有方法“nothover”

为什么是这样?如果有人可以提供帮助,我将不胜感激。我所说的“帮助”是指解释一些 javascript 函数以及如何确保它们正常运行。

谢谢

Bra*_*don 5

setTimeout返回一个值。如果您想“取消”超时,请向 提供值clearTimeout。您没有将您的函数传递给clearTimeout. 那不会有任何作用。该线程中似乎没有人注意到这一点。这是关于此的文档:clearTimeout

这是工作代码:

var notHover = function() {
   var timerId = setTimeout(function () {
      $('#proFBfeel').hide();
      $('#proFBfeel .moreinfos').html('');
      $('.contact_pro').html('');  
    }, 3000);

    // return a function which will cancel the timer
    return function () { clearTimeout(timerId); };
};

// start the timer.  We can call cancelTimer() if we want to cancel it
var cancelTimer = notHover();

$('#proFBfeel').hover(function() {
    // mouse in, cancel the notHoverTimer
    if (cancelTimer) {
        cancelTimer();
        cancelTimer= undefined;
    }
}, function () {
    // mouse out, start the timer again
    cancelTimer = notHover();
});
Run Code Online (Sandbox Code Playgroud)

````