JavaScript實(shí)現(xiàn)的一個(gè)倒計(jì)時(shí)的類
近期在做排列五的彩票項(xiàng)目,每一期都有購彩時(shí)段,即用戶打開這個(gè)排列五的頁面時(shí),會(huì)從服務(wù)器傳來一個(gè)remaintime(離本次彩期結(jié)束的剩余時(shí)間),然后這個(gè)時(shí)間在客戶端遞減呈現(xiàn)給用戶看,讓用戶獲得本次彩期的剩余時(shí)間。
實(shí)現(xiàn)原理挺簡單的,在此不在贅述,運(yù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>小時(shí)<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é)點(diǎn)的id container: obj.container || 'container', remaintime: Number(obj.remaintime) || 0, //倒計(jì)時(shí)完成后的回調(diào) callback: obj.callback || new Function }; return obj; }, resetTime: function(){ var that = this; that.container.innerHTML = "已經(jīng)截止"; }, //刷新時(shí)間 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--; } } } //時(shí)刻監(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; }, //對(duì)于位數(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ù)器返回的剩余時(shí)間,單位為秒 remaintime: 10000, //倒計(jì)時(shí)完成時(shí)的回調(diào) callback: function(){ document.getElementById('remaintime').innerHTML = '已截止'; } }); </script> </body> </html>
- javascript倒計(jì)時(shí)功能實(shí)現(xiàn)代碼
- javascript 實(shí)現(xiàn) 秒殺,團(tuán)購 倒計(jì)時(shí)展示的記錄 分享
- Javascript倒計(jì)時(shí)頁面跳轉(zhuǎn)實(shí)例小結(jié)
- javascript實(shí)現(xiàn)促銷倒計(jì)時(shí)+fixed固定在底部
- JavaScript分秒倒計(jì)時(shí)器實(shí)現(xiàn)方法
- C#結(jié)合JavaScript實(shí)現(xiàn)秒殺倒計(jì)時(shí)的方法
- JavaScript實(shí)現(xiàn)簡單的數(shù)字倒計(jì)時(shí)
- javascript實(shí)現(xiàn)倒計(jì)時(shí)并彈窗提示特效
- javascript實(shí)現(xiàn)倒計(jì)時(shí)(精確到秒)
- javascript實(shí)現(xiàn)下班倒計(jì)時(shí)效果的方法(可桌面通知)
- javascript頁面倒計(jì)時(shí)實(shí)例
- javascript同步服務(wù)器時(shí)間和同步倒計(jì)時(shí)小技巧
相關(guān)文章
通過實(shí)例了解Render Props回調(diào)地獄解決方案
這篇文章主要介紹了通過實(shí)例了解Render Props回調(diào)地獄解決方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11JavaScript實(shí)現(xiàn)PC端四格密碼輸入框功能
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)PC端四格密碼輸入框功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-02-02在JavaScript中正確引用bind方法的應(yīng)用
本文的重點(diǎn)在于討論bind()方法的實(shí)現(xiàn),在開始討論bind()的實(shí)現(xiàn)之前,我們先來看看bind()方法的使用,有需要的小伙伴可以參考下。2015-05-05JavaScript實(shí)現(xiàn)動(dòng)態(tài)網(wǎng)頁時(shí)鐘
這篇文章主要介紹了JavaScript實(shí)現(xiàn)動(dòng)態(tài)網(wǎng)頁時(shí)鐘,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06JS實(shí)現(xiàn)消息來時(shí)讓網(wǎng)頁標(biāo)題閃動(dòng)效果的方法
這篇文章主要介紹了JS實(shí)現(xiàn)消息來時(shí)讓網(wǎng)頁標(biāo)題閃動(dòng)效果的方法,實(shí)例分析了JavaScript基于時(shí)間函數(shù)動(dòng)態(tài)操作頁面元素的技巧,并附帶了開心網(wǎng)的標(biāo)題閃動(dòng)解決方案,需要的朋友可以參考下2016-04-04