hover / setInterval:未定义的时间间隔

时间:2011-04-11 15:21:38

标签: jquery hover setinterval


我尝试对同一个div使用'hover'函数和setInterval:如果我们将div悬停,则setInterval启动。
但我有一个问题:似乎对于2个不同的div,当我悬停第二个div时,它首先'等待'1000毫秒然后开始第二个div的'setInterval'。所以当我悬停第二个div时,它们都保持当前的图像,过了一会儿,第二个div的setInterval开始了。 (我希望它很快,而不是等待那些ms)。

你知道我怎么做吗? 这是我的代码:

var intval;    
    img.bind('mouseenter', function(){   
        var that = $(this);  
    intval = setInterval(function(){  
        next(that);  
    },1000);  
}).bind('mouseleave', function(){  
    var that = $(this);  
    clearInterval(intval);  
}); 

非常感谢。

1 个答案:

答案 0 :(得分:0)

通过在next()事件中调用mouseenter,它应该有效。

var intval;    

img.bind('mouseenter', function(){   
    var that = $(this);  
    next(that);
    intval = setInterval(function(){  
        next(that);  
    }, 1000);  
}).bind('mouseleave', function(){  
    var that = $(this);  
    clearInterval(intval);  
});