您的位置:首页 > 其它

定时器遇到的坑

2016-03-04 19:21 330 查看
// 商品倒计时用一个定时器来写
//type 是不同页面
var timecountdown = {
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"));
}else{
var endTime=this.num;
}
var key = parseInt(this.getAttribute("itemid"));

if (endTime < 0) {
return true; //跳过此次循环
} else {
endTime = endTime - 1000;
}
this.num=endTime;
_that.clock_jx(key, endTime, type);
});

setTimeout(function() {
_that.updateEndTime(type);
if(!_that.timehms){
_that.hmstime();
}
}, 1000);
}

timecountdown.clock_jx = function(key, diff, skin) {
var _that = this;

if (diff <= 0) {
$("#leftTimeJx" + key).parent().hide();
$("#leftTimeJx" + key).parent().parent().find('.jx-ing').show();
$.post('/yunbuy/lottery', 'skin=' + skin + '&id=' + key, function(data) {
setTimeout(function() {
$('#itemDjx' + key).remove();
$('#win-list .item-db').eq(0).before(data.html);
}, 5000);
}, 'json');
} else {
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 (DifferSecond.toString().length < 2) DifferSecond = '0' + DifferSecond;
if (DifferMinute.toString().length < 2) DifferMinute = '0' + DifferMinute;

var sTime = "";
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.hmstime = function() {
//毫秒单独计时
var _that = this;

clearInterval(_that.timehms);
_that.timehms = setInterval(function() {

if (_that.h <= 0) _that.h = 100;
_that.h = parseInt(_that.h) - 1;
if (_that.h.toString().length < 1) _that.h = '00';
if (_that.h.toString().length == 1) _that.h = '0' + _that.h;
if (_that.h.toString().length > 2) _that.h = '99';
setTimeout(function() {
if ($('.timeHm').length == 0) {
clearInterval(_that.timehms);
_that.timehms=false; //此处一定要赋值,不然下次 再次开启 我用的这个值得判断 就会不准,虽然定时器清了,但是那个值一直存在,可以说是定时器id
};
}, 5000);

$('.timeHm').html(_that.h)
}, 15);
}

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();


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: