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; //此处一定要赋值,不然下次 再次开启 我用的这个值得判断 就会不准,虽然定时器清了,但是那个值一直存在,可以说是定时器id 82 }; 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 会越来愈大。占用内存,小心内存泄露。因此 商城 多个计算 最好用循环,一次定时器搞定
,一个方法里最好只开一个定时器,再开之前先清定时器。方式定时器过多,控制不过来
浏览器机制 定时器的假死状态
定时器有一个问题,就是当页面中开一个定时器,然后你没有关闭页面,继续浏览其他网页,过一会(几分钟)你在浏览这个定时器,这个定时器,在这期间是假死状态,就是不执行,你在返回来看到的估计就是定时器只进行了几秒。
解决方案:把后台传过来的时间段,转化为本地时间几点过期,然后一直判断是否到达本地时间即可,解决这个bug
// 商品倒计时用一个定时器来写 //type 是不同页面 var timecountdown = { hours_jx: 60 * 60 * 1000, Secondms_jx: 60 * 1000, minutems_jx: 1000, h: 100, timehms: false }; timecountdown.updateEndTime = function(type) { var _that = this; $(".item-djx").each(function(i, el) { if(!this.num){ var endTime = parseInt(this.getAttribute("endTime"))+new Date().getTime(); this.num=endTime; }else{ var endTime=this.num; } var key = parseInt(this.getAttribute("itemid")); var duringtime=endTime-new Date().getTime(); duringtime = duringtime - 1000; if (duringtime <= 0) { if(this.onoff){ return true; //跳过此次循环 } $("#leftTimeJx" + key).parent().hide(); $("#leftTimeJx" + key).parent().parent().find('.jx-ing').show(); $.post('/yunbuy/lottery', 'skin=' + type + '&id=' + key, function(data) { setTimeout(function() { $('#itemDjx' + key).remove(); $('#win-list .item-db').eq(0).before(data.html); }, 5000); }, 'json'); this.onoff=true; } else { _that.clock_jx(key,duringtime); } }); setTimeout(function() { _that.updateEndTime(type); }, 1000); } timecountdown.clock_jx = function(key, diff, skin) { var _that = this; var Differhours = Math.floor(diff / _that.hours_jx); diff -= Differhours * _that.hours_jx; var DifferSecond = Math.floor(diff / _that.Secondms_jx); diff -= DifferSecond * _that.Secondms_jx; var DifferMinute = Math.floor(diff / _that.minutems_jx); diff -= DifferMinute * _that.minutems_jx; if (Differhours.toString().length < 2) Differhours = '0' + Differhours; if (DifferSecond.toString().length < 2) DifferSecond = '0' + DifferSecond; if (DifferMinute.toString().length < 2) DifferMinute = '0' + DifferMinute; var sTime = ""; if(Differhours!='00'){ sTime += "<span>" + Differhours + "</span><b>:</b>"; } sTime += "<span>" + DifferSecond + "</span><b>:</b>"; sTime += "<span>" + DifferMinute + "</span><b>:</b>"; sTime += "<span class='timeHm'>" + 99 + "</span>"; document.getElementById("leftTimeJx" + key).innerHTML = sTime; //显示倒计时信息 }
timecountdown.updateEndTime();