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();
原文:http://www.cnblogs.com/yangjingjing/p/5243163.html