星空网 > 软件开发 > Java

定时器遇到的坑

 1 // 商品倒计时用一个定时器来写 2 //type 是不同页面 3 var timecountdown = { 4   Secondms_jx: 60 * 1000, 5   minutems_jx: 1000, 6   h: 100, 7   timehms: false 8 }; 9 10 timecountdown.updateEndTime = function(type) {11   var _that = this;12   $(".item-djx").each(function(i, el) {13     if(!this.num){14       var endTime = parseInt(this.getAttribute("endTime"));15     }else{16       var endTime=this.num;17     }    18     var key = parseInt(this.getAttribute("itemid"));19 20 21     if (endTime < 0) {22       return true; //跳过此次循环23     } else {24       endTime = endTime - 1000;25     }26     this.num=endTime;27     _that.clock_jx(key, endTime, type);28   });29 30   setTimeout(function() {31     _that.updateEndTime(type);32     if(!_that.timehms){33       _that.hmstime();34     }35   }, 1000);36 }37 38 timecountdown.clock_jx = function(key, diff, skin) {39   var _that = this;40 41   if (diff <= 0) {42     $("#leftTimeJx" + key).parent().hide();43     $("#leftTimeJx" + key).parent().parent().find('.jx-ing').show();44     $.post('/yunbuy/lottery', 'skin=' + skin + '&id=' + key, function(data) {45       setTimeout(function() {46         $('#itemDjx' + key).remove();47         $('#win-list .item-db').eq(0).before(data.html);48       }, 5000);49     }, 'json');50   } else {51     var DifferSecond = Math.floor(diff / _that.Secondms_jx);52     diff -= DifferSecond * _that.Secondms_jx;53     var DifferMinute = Math.floor(diff / _that.minutems_jx);54     diff -= DifferMinute * _that.minutems_jx;55 56     if (DifferSecond.toString().length < 2) DifferSecond = '0' + DifferSecond;57     if (DifferMinute.toString().length < 2) DifferMinute = '0' + DifferMinute;58 59     var sTime = "";60     sTime += "<span>" + DifferSecond + "</span><b>:</b>";61     sTime += "<span>" + DifferMinute + "</span><b>:</b>";62     sTime += "<span class='timeHm'>" + 99 + "</span>";63     document.getElementById("leftTimeJx" + key).innerHTML = sTime; //显示倒计时信息64   }65 }66 timecountdown.hmstime = function() {67   //毫秒单独计时68   var _that = this;69 70   clearInterval(_that.timehms);71   _that.timehms = setInterval(function() {72 73     if (_that.h <= 0) _that.h = 100;74     _that.h = parseInt(_that.h) - 1;75     if (_that.h.toString().length < 1) _that.h = '00';76     if (_that.h.toString().length == 1) _that.h = '0' + _that.h;77     if (_that.h.toString().length > 2) _that.h = '99';78     setTimeout(function() {79       if ($('.timeHm').length == 0) {80       clearInterval(_that.timehms);81       _that.timehms=false; //此处一定要赋值,不然下次 再次开启 我用的这个值得判断 就会不准,虽然定时器清了,但是那个值一直存在,可以说是定时器id82       };83     }, 5000);84 85     $('.timeHm').html(_that.h)86   }, 15);87 }88 89 timecountdown.hmstime();

清空定时器之后 输出timehms  这个值一直存在 而且占用内存  
 
for(var i=0;i<10;i++){
    var a=setInterval("1",10);
    console.log(a);
}
这个定时器其实开了10个,但是只有最后一个有名字,能控制,其他都控制不了,而且清不掉,占用内存。
会看到 开多少个定时器,他的id 会越来愈大。占用内存,小心内存泄露。因此 商城 多个计算 最好用循环,一次定时器搞定
,一个方法里最好只开一个定时器,再开之前先清定时器。方式定时器过多,控制不过来
 
 
 
 
浏览器机制  定时器的假死状态
定时器有一个问题,就是当页面中开一个定时器,然后你没有关闭页面,继续浏览其他网页,过一会(几分钟)你在浏览这个定时器,这个定时器,在这期间是假死状态,就是不执行,你在返回来看到的估计就是定时器只进行了几秒。
解决方案:暂无,以后有了再添加



原标题:定时器遇到的坑

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

跨境电商保险政策:https://www.goluckyvip.com/tag/35416.html
跨境电商保险种类:https://www.goluckyvip.com/tag/35417.html
跨境电商报告:https://www.goluckyvip.com/tag/35418.html
跨境电商报告总结:https://www.goluckyvip.com/tag/35419.html
长期仓储费:https://www.goluckyvip.com/tag/3542.html
跨境电商报关:https://www.goluckyvip.com/tag/35420.html
83万单,Halara的TikTok美国小店爆单思路拆解:https://www.goluckyvip.com/news/186217.html
携程高铁旅游攻略,更优惠的高铁票价都在哪里:https://www.vstour.cn/a/362188.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流