javascript时间事件问题(setTimeout/clearTimeout)

javascript时间事件问题(setTimeout/clearTimeout),javascript,jquery,timer,settimeout,Javascript,Jquery,Timer,Settimeout,我在处理时间事件时总是遇到困难。有人能解释一下为什么A不工作而B工作吗?唯一的区别在于我将事件绑定放在函数中。不要担心函数关闭,它与问题无关。当我测试A时,并没有js错误,但计时器并没有被清除 A-> B-> 编辑:请忽略容器部分,它与问题无关,它只是我没有取出的完整代码的一部分您对的使用在第一种方法的错误范围内 试一试 后来 var container = self; 在代码中,例如A $('.hover_container').on('mouseleave', function(e) {

我在处理时间事件时总是遇到困难。有人能解释一下为什么A不工作而B工作吗?唯一的区别在于我将事件绑定放在函数中。不要担心函数关闭,它与问题无关。当我测试A时,并没有js错误,但计时器并没有被清除

A->

B->


编辑:请忽略容器部分,它与问题无关,它只是我没有取出的完整代码的一部分

您对
的使用在第一种方法的错误范围内

试一试

后来

var container = self;
在代码中,例如A

$('.hover_container').on('mouseleave', function(e) {
 // set the close timer
 var container = this;
实际上是指当前的
$('.hover_container')
元素


另外,由于
setTimeout
将在上一个
setTimeout
完成之前等待重新启动,因此可能会出现差异。您可能希望切换到
setInterval
,因为它将在每个间隔集上发出回调,而不管上一次回调是否已完成。

A在调用init的对象存在之前被绑定。因为您返回了一个新对象。如果使用,将创建两个对象。1与vars en绑定。和1的回报

B工作是因为您创建了一个初始化元素并使用正确范围的函数。A不起作用,因为绑定位于错误的作用域上,因为您创建了2个对象:

new Test.Navigation(); // Create 1 object

// Create second object.
return {
    init : function() {
        addListeners();
    }
};
你最好得到一个这样的结构,然后它也应该工作:

Test.Navigation = (function() {
    // Private vars. Use underscore to make it easy for yourself so they are private.
    var _openTimer = null,
        _closeTimer = null;

    $('.hover_container').on('mousemove', function(e) {
        clearTimeout(_closeTimer );
    });

    $('.hover_container').on('mouseleave', function(e) {

        // set the close timer, 
        //    use $.proxy so you don't need to create a exta var for the container.
        _closeTimer = setTimeout(
                $.proxy(function() {
                    //has the mouse paused
                    close(this);
                }, this)
            , 750);

    });


    this.addListeners = function() {
        // nothing here
    };

    this.init = function() {
        this.addListeners();
    }

    // Always call the init?
    this.init();

    return this; // Return the new object Test.Navigation
})();
像这样使用它

var nav = new Test.Navigation();
nav.init();

正如你所见,我对你的代码进行了一些升级。对私有变量使用
$.proxy

我猜在调用代码中,您有一个语句
new Test.Navigation()
,对于B,addListeners在新测试时被调用。Navigation()。在中,返回一个调用init函数的对象ref。能否验证是否调用了init()


也就是说,在添加处理程序之前,必须调用init()。在B中,每次实例化Test.Navigation时都会添加处理程序——根据调用代码的不同,如果一次实例化多个Test.Navigation(),则处理程序可能不好。

对单个计时实例使用
setTimeout
。使用
setInterval
进行持续计时。声明
var container=this在容器的直接作用域中,而不是在另一个函数中。在A中,容器不是您所需要的。@dystroy容器是我所需要的,这与问题无关,如果清除计时器工作,关闭功能将不会启动,我得到您所说的间隔,但容器正是我想要使用的,因为我想关闭它,无论如何,如果正确触发清除系紧,关闭功能甚至不会启动。此外,A区的容器与B区的容器相同,感谢您将注意力集中在所提出的问题上,解释清楚
new Test.Navigation(); // Create 1 object

// Create second object.
return {
    init : function() {
        addListeners();
    }
};
Test.Navigation = (function() {
    // Private vars. Use underscore to make it easy for yourself so they are private.
    var _openTimer = null,
        _closeTimer = null;

    $('.hover_container').on('mousemove', function(e) {
        clearTimeout(_closeTimer );
    });

    $('.hover_container').on('mouseleave', function(e) {

        // set the close timer, 
        //    use $.proxy so you don't need to create a exta var for the container.
        _closeTimer = setTimeout(
                $.proxy(function() {
                    //has the mouse paused
                    close(this);
                }, this)
            , 750);

    });


    this.addListeners = function() {
        // nothing here
    };

    this.init = function() {
        this.addListeners();
    }

    // Always call the init?
    this.init();

    return this; // Return the new object Test.Navigation
})();
var nav = new Test.Navigation();
nav.init();