javascript - 清楚定时器清除不了

查看:186
本文介绍了javascript - 清楚定时器清除不了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

   return (function(){
                    timers=null;
                   
                    if(opts.autopay){
                        slider.autopay(obj,opts);
                    }
                    slider.clearTimers(obj,opts,timers);
                })()
   
    autopay:function(obj,opts){
                   return (function () {
                       if(opts.times&&opts.autopay){
                               clearInterval(timers);
                               timers==null;
                              if(timers==null){
                                  timers=setInterval(function () {
                                      slider._right(obj,opts);
                                  },opts.times);
                              }else{
                                 console.log(timers);//这里返回有数值,上面明明清除了定时器
                              }
                       }
                   })();
                },
                clearTimers:function(obj,opts,timers){
                   return (function () {
                           obj.hover(function(){
    //                           opts.autopay=false;
                               clearInterval(timers);
                           },function(){
    //                           opts.autopay=true;
                               slider.autopay(obj,opts);
                           });
                   })();
                }

解决方案

修改答案: timer记录的是定时器的一个数值,清除时是按照数值清除的,并不会将timers变为null


timers==null;
if(timers==null){
    timers=setInterval(function () {
        slider._right(obj,opts);
    },opts.times);
}else{
    console.log(timers);//这里返回有数值,上面明明清除了定时器
}

你确定进入了console.log?应该是永远都进不了吧

这篇关于javascript - 清楚定时器清除不了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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