鼠标退出时的clearTimeout [英] clearTimeout on Mouse Exit

查看:98
本文介绍了鼠标退出时的clearTimeout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的计时器设置:

I have a timer setup like so:

var timerID;

$this.hover(function(){
   $this.find('.stage_obj').each(function(index){
      var current_obj = $(this);
      timerID = setTimeout(function(){
         animate_on_top(current_obj, index);}, parseInt(OS.delay_y_on_set[index],10));
      });   
}, function(){
   clearTimeout(timerID);
});

有一些功能可以控制悬停时动画的进/出.计时器起到了延迟的作用(.delay在我的情况下不起作用).一切正常,除了没有在鼠标退出时取消计时器而仍然触发.这是实际的animation_on函数,称为:

There are functions to control the animation in/out on hover. The timers are acting as delays (.delay won't work in my situation). Everything works fine, except the timer isn't cancelled on mouse exit, and still fires. Here's the actual animation_on function being called:

function animate_on_top(current_obj, index){ 
   current_obj.animate(
      {'top':OS.ends_y_set[index]},
      {duration:500, queue:false,
      specialEasing:{'top':'linear'}
});

任何人都知道为什么setTimeout不取消计时器吗?谢谢!

Anyone have any ideas why the setTimeout isn't cancelling the timer? Thanks!

推荐答案

之所以没有清除超时,是因为您通过each设置了多个超时,但是仅存储(因此清除了)其中一个.您需要存储并清除您创建的每个超时ID.

The reason why the timeout isn't cleared is because you set multiple timeouts via the each but only store (and hence clear) one of them. You need to store and clear each of the time out ids you create.

var timerID = [];

$this.hover(function(){
   $this.find('.stage_obj').each(function(index){
      var current_obj = $(this);
      var currentTimerID = setTimeout(function(){
         animate_on_top(current_obj, index);}, parseInt(OS.delay_y_on_set[index],10));
      timerID.push(currentTimerID);
      });   
 }, function(){
   for (var i = 0; i < timerID.length; i++) {
     clearTimeout(timerID[i]);
   }
});

这篇关于鼠标退出时的clearTimeout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆