国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

JavaScript實現(xiàn)的一個倒計時的類

2019-11-20 12:57:47
字體:
供稿:網(wǎng)友

近期在做排列五的彩票項目,每一期都有購彩時段,即用戶打開這個排列五的頁面時,會從服務(wù)器傳來一個remaintime(離本次彩期結(jié)束的剩余時間),然后這個時間在客戶端遞減呈現(xiàn)給用戶看,讓用戶獲得本次彩期的剩余時間。

實現(xiàn)原理挺簡單的,在此不在贅述,運行以下代碼查看demo:

<!doctype html><html><head><meta http-equiv="Content-Type" content="text/html; charset=gbk" /><title>index</title><style type="text/css">em{color:#f00;}</style></head><body><div id="remaintime"></div><script type="text/javascript">var TheTime = function(){ this.init.apply(this,arguments);};TheTime.prototype = { init: function(obj){ var that = this; obj = that.buildParam(obj); that.callback = obj.callback; var container = that.container = document.getElementById(obj.container); container.innerHTML = '<em></em>小時<em></em>分鐘<em></em>秒'; var hourSpace = that.hourSpace = container.getElementsByTagName('em')[0]; var minuteSpace = that.minuteSpace = container.getElementsByTagName('em')[1]; var secondSpace = that.secondSpace = container.getElementsByTagName('em')[2]; if(obj.remaintime==0){  that.resetTime();  return; } that.hours = Math.floor(obj.remaintime/3600); that._remainder1 = obj.remaintime % 3600; that.minutes = Math.floor(that._remainder1/60); that.seconds = that._remainder1 % 60; var timer = that.timer = setInterval(function(){  that.renderTime.apply(that); },1000); }, buildParam: function(obj){ obj = {  //container為dom節(jié)點的id  container: obj.container || 'container',  remaintime: Number(obj.remaintime) || 0,  //倒計時完成后的回調(diào)  callback: obj.callback || new Function }; return obj; }, resetTime: function(){ var that = this; that.container.innerHTML = "已經(jīng)截止"; }, //刷新時間 renderTime: function(){ //debugger; var that = this; if(that.seconds>0){  that.seconds--; }else{  that.seconds = 59;  if(that.minutes>0){  that.minutes--;  }else{  that.minutes = 59;  if(that.hours>0){   that.hours--;  }  } }  //時刻監(jiān)聽 if(that.hours==0 && that.minutes==0 && that.seconds==0){  //執(zhí)行回調(diào)  that._callback(); } var bitHandle = that.bitHandle; var _hour = bitHandle(that.hours); var _minute = bitHandle(that.minutes); var _second = bitHandle(that.seconds); that.hourSpace.innerHTML = _hour; that.minuteSpace.innerHTML = _minute; that.secondSpace.innerHTML = _second; }, //對于位數(shù)的處理,確保返回兩位數(shù) bitHandle: function(num){ var str = num.toString(); if(str.length<2){  str = 0 + str; } return str; }, _callback: function(){ var that = this; clearInterval(that.timer); that.callback(); }};new TheTime({ //容器id container: 'remaintime', //服務(wù)器返回的剩余時間,單位為秒 remaintime: 10000, //倒計時完成時的回調(diào) callback: function(){ document.getElementById('remaintime').innerHTML = '已截止'; }});</script></body></html>
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 金塔县| 柯坪县| 改则县| 曲靖市| 江安县| 崇左市| SHOW| 当涂县| 峡江县| 通化县| 沙洋县| 黑龙江省| 孟津县| 和静县| 舒兰市| 仁寿县| 神池县| 孝感市| 高陵县| 威信县| 东源县| 延安市| 永安市| 垣曲县| 肃宁县| 扬州市| 阿克苏市| 水富县| 无为县| 夏邑县| 永仁县| 元氏县| 乌兰浩特市| 济源市| 屏东县| 雷山县| 高要市| 定边县| 金乡县| 象山县| 阿巴嘎旗|