定时器遇到的坑
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();
相关文章推荐
- 软件项目管理第一次作业之完整项目的经历by石家名(3013218062)
- #include<sys/types.h>
- 素数求和问题
- eclipse buildPath及发布配置
- 常用的数据结构
- lintcode :同构字符串
- UVA 10054 The Necklace(欧拉回路)
- SpringMVC使用注解驱动的依赖注入
- 文本挖掘之文本聚类算法之PAM(k-中心点)
- Ajax实例
- hibernate笔记--继承映射关系的三种实现方式
- JION,LEFT JION RIGHT JION FULL JION 联系与区别
- Linux多线程编程的时候如何查看一个进程中的某个线程是否存活
- HDU 2816 I Love You Too [Ad Hoc]
- 大数加法
- 森林
- ArcGIS API for JavaScript 4.0尝鲜——WebGIS前端开发大杀器
- 七牛云图片处理实践之GIF|如何解决GIF流量爆炸问题?
- Gprinter Android SDK V2.1.4 使用说明
- 连接查询 两张表里有同名字段的问题