JavaScript實(shí)現(xiàn)的一個(gè)倒計(jì)時(shí)的類
近期在做排列五的彩票項(xiàng)目,每一期都有購(gòu)彩時(shí)段,即用戶打開這個(gè)排列五的頁(yè)面時(shí),會(huì)從服務(wù)器傳來一個(gè)remaintime(離本次彩期結(jié)束的剩余時(shí)間),然后這個(gè)時(shí)間在客戶端遞減呈現(xiàn)給用戶看,讓用戶獲得本次彩期的剩余時(shí)間。
實(shí)現(xiàn)原理挺簡(jiǎ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)購(gòu) 倒計(jì)時(shí)展示的記錄 分享
- Javascript倒計(jì)時(shí)頁(yè)面跳轉(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)簡(jiǎn)單的數(shù)字倒計(jì)時(shí)
- javascript實(shí)現(xiàn)倒計(jì)時(shí)并彈窗提示特效
- javascript實(shí)現(xiàn)倒計(jì)時(shí)(精確到秒)
- javascript實(shí)現(xiàn)下班倒計(jì)時(shí)效果的方法(可桌面通知)
- javascript頁(yè)面倒計(jì)時(shí)實(shí)例
- javascript同步服務(wù)器時(shí)間和同步倒計(jì)時(shí)小技巧
相關(guān)文章
Javascript中的方法鏈(Method Chaining)介紹
這篇文章主要介紹了Javascript中的方法鏈(Method Chaining)介紹,本文講解了Javascript Method Chaining、Method Chaining 使用、Method Chaining VS prototype Chaining等內(nèi)容,需要的朋友可以參考下2015-03-03
JavaScript Event學(xué)習(xí)第二章 Event瀏覽器兼容性
在這一章我將對(duì)重要的事件做一個(gè)概述,包括一些流行的瀏覽器的兼容性問題。2010-02-02
JavaScript 字符串處理函數(shù)使用小結(jié)
JavaScript 字符串處理函數(shù)使用小結(jié),學(xué)習(xí)js的朋友可以參考下。2010-12-12
整理CocosCreator常用知識(shí)點(diǎn)
這篇文章主要介紹了整理CocosCreator常用知識(shí)點(diǎn),這些知識(shí)點(diǎn),平時(shí)幾乎都能用到,希望同學(xué)們看完后,可以自己去試一下,加深印象2021-04-04
Javascript實(shí)現(xiàn)Web顏色值轉(zhuǎn)換
這篇文章主要介紹了Javascript實(shí)現(xiàn)Web顏色值轉(zhuǎn)換,需要的朋友可以參考下2015-02-02
node.js Web應(yīng)用框架Express入門指南
這篇文章主要介紹了node.js Web應(yīng)用框架Express入門指南,從安裝到各種技術(shù)的應(yīng)用,都進(jìn)行了講解,是一篇不錯(cuò)的Express入門教程,需要的朋友可以參考下2014-05-05

