純js實(shí)現(xiàn)倒計(jì)時(shí)功能
通過js實(shí)現(xiàn)頁面的倒計(jì)時(shí)功能。
思路: 傳入一個(gè)秒數(shù)c,c/60可以得到分鐘m, c%60可以得到顯示的秒數(shù)s,同理,再將m/60可是得到小時(shí)數(shù), m/%可以得到分鐘數(shù)。通過setInterval每次將總秒數(shù)-1,并將計(jì)算所得時(shí)間顯示到頁面上。
第一版的骯臟代碼如下, 可以作為反面教材思考一下
<html>
<head>
<title>Tomato</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
var vTimeLength = 5;
var vHour;
var vMinutes;
var vSeconds;
var vRemainingTime;
function countDown(){
vTimeLength = vTimeLength - 1;
vMinutes = Math.floor(vTimeLength/60);
vSeconds = Math.floor(vTimeLength%60);
if (vMinutes >= 60){
vHour = Math.floor(vMinutes/60);
var vMinutesNew = Math.floor(vMinutes%60);
vRemainingTime = vHour + ":" + vMinutesNew + ":" + vSeconds;
} else {
vRemainingTime = vMinutes + ":" + vSeconds;
}
document.getElementById("div_countDown").innerHTML = vRemainingTime;
if (vTimeLength < 1) {
alert('do sth');
}
}
</script>
</head>
<body>
<div id="div_countDown"></div>
<script type="text/javascript">
setInterval("countDown()", 1000);
</script>
</body>
</html>
缺陷:
1、定義了眾多的全局變量,
2、沒有復(fù)用性,
3、setInterval容易導(dǎo)致隊(duì)列過多, 結(jié)束事件如果是非阻塞事件, 倒計(jì)時(shí)會(huì)繼續(xù)執(zhí)行出現(xiàn)負(fù)數(shù),
4、不符合面向?qū)ο笏枷?。?!?/span>
針對(duì)缺陷1的解決方案是, 定義一個(gè)函數(shù), 將相關(guān)全局變量放到函數(shù)內(nèi)部,使之成為局部變量
針對(duì)缺陷2:為函數(shù)指定參數(shù),提高復(fù)用性。 這里定義了3個(gè)參數(shù)vTimeLength為倒計(jì)時(shí)總秒數(shù),showTagId為顯示到頁面元素的id, callback為倒計(jì)時(shí)結(jié)束后的回掉方法
針對(duì)缺陷3:用setTimeout替代setInterval
優(yōu)化后的代碼如下:
<html>
<head>
<title>countdown</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
function countDown(vTimeLength, showTagId, callback) {
var vHour;
var vMinutes;
var vMinutesNew
var vSeconds;
var vRemainingTime;
function countDownInner(vTimeLength){
vMinutes = Math.floor(vTimeLength/60);
vSeconds = Math.floor(vTimeLength%60);
if (vMinutes >= 60){
vHour = Math.floor(vMinutes/60);
vMinutesNew = Math.floor(vMinutes%60);
vRemainingTime = vHour + ":" + vMinutesNew + ":" + vSeconds;
} else {
vRemainingTime = vMinutes + ":" + vSeconds;
}
document.getElementById(showTagId).innerHTML = vRemainingTime;
vTimeLength = vTimeLength - 1;
if (vTimeLength > 0) {
setTimeout(function(){countDownInner(vTimeLength);}, 1000);
} else {
callback();
}
}
countDownInner(vTimeLength);
}
</script>
</head>
<body>
<div id="div_countDown"></div>
<script type="text/javascript">
countDown(5, "div_countDown", function(){alert('do sth');});
</script>
</body>
</html>
這里有一點(diǎn)需要注意
setTimeout(function(){countDownInner(vTimeLength);}, 1000);
第一次我將此句寫成了
setTimeout(countDownInner(vTimeLength), 1000);
結(jié)果函數(shù)直接執(zhí)行了, 沒有等待1秒的時(shí)間。如果沒有入?yún)ⅲ?即setTimeout("countDownInner()", 1000); 則可正常執(zhí)行。
至于前面提到的不夠面向?qū)ο蟮娜毕荩?也是剛剛接觸, 這里貼出代碼,希望能夠互相交流
<html>
<head>
<title>count_down</title>
<script type="text/javascript">
var countDown = {
flag: true,
hour: 0,
minutes: 0,
minutesNew: 0,
seconds: 0,
show: 0,
current: 0,
length: 0,
showTagId: null,
// callback: null,
countDownInner: function(vTimeLength){
if (!this.flag) {
return;
}
var that=this;
this.current = vTimeLength;
minutes = Math.floor(vTimeLength/60);
seconds = Math.floor(vTimeLength%60);
if (minutes >= 60){
hour = Math.floor(minutes/60);
minutesNew = Math.floor(minutes%60);
show = hour + ":" + minutesNew + ":" + seconds;
} else {
show = minutes + ":" + seconds;
}
document.getElementById(this.showTagId).innerHTML = show;
vTimeLength = vTimeLength - 1;
if (vTimeLength > 0) {
setTimeout(function(){that.countDownInner(vTimeLength);}, 1000);
} else {
setTimeout(function(){that.callback();}, 1000);
}
},
run: function(vTimeLength, showTagId, callback) {
if (!this.flag) {
this.flag = true;
this.countDownInner(this.current);
} else if (showTagId) {
this.length = vTimeLength;
this.showTagId = showTagId;
this.callback = callback;
this.countDownInner(vTimeLength);
}
},
stop: function(){
this.flag = false;
},
restart: function(){
this.flag = true;
this.countDownInner(this.length);
}
};
function countDownStart() {
countDown.run();
}
function countDownStop() {
countDown.stop();
}
</script>
</head>
<body>
<div id="div_countDown"></div>
<script type="text/javascript">
countDown.run(5, 'div_countDown',function(){alert('12')});
</script>
<span>
<button onclick="countDownStart();">start</button>
<button onclick="countDownStop();">stop</button>
</span>
</body>
</html>
一個(gè)難點(diǎn)是this的使用, 在函數(shù)內(nèi)部, this是調(diào)用當(dāng)前函數(shù)范圍,所以setTimeout(function(){this.countDownInner(vTimeLength);}, 1000);會(huì)出現(xiàn)undefined。
解決方案是定義一個(gè)that變量接收外部函數(shù)的this指針,然后通過that即可調(diào)用外部域。
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持腳本之家!
- 純jsp實(shí)現(xiàn)的倒計(jì)時(shí)動(dòng)態(tài)顯示效果完整代碼
- JS實(shí)現(xiàn)倒計(jì)時(shí)(天數(shù)、時(shí)、分、秒)
- PHP+JS實(shí)現(xiàn)的商品秒殺倒計(jì)時(shí)用法示例
- js制作支付倒計(jì)時(shí)頁面
- JavaScript實(shí)現(xiàn)刷新不重記的倒計(jì)時(shí)
- js實(shí)現(xiàn)精確到毫秒的倒計(jì)時(shí)效果
- javascript特效實(shí)現(xiàn)——當(dāng)前時(shí)間和倒計(jì)時(shí)效果的簡(jiǎn)單實(shí)例
- JS/jQ實(shí)現(xiàn)免費(fèi)獲取手機(jī)驗(yàn)證碼倒計(jì)時(shí)效果
- 點(diǎn)擊按鈕出現(xiàn)60秒倒計(jì)時(shí)的簡(jiǎn)單js代碼(推薦)
- js代碼實(shí)現(xiàn)點(diǎn)擊按鈕出現(xiàn)60秒倒計(jì)時(shí)
相關(guān)文章
微信小程序?qū)崿F(xiàn)跟隨菜單效果和循環(huán)嵌套加載數(shù)據(jù)
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)跟隨菜單效果和循環(huán)嵌套加載數(shù)據(jù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11
JavaScript實(shí)現(xiàn)導(dǎo)入和導(dǎo)出Excel的示例詳解
在現(xiàn)代的Web應(yīng)用開發(fā)中,與Excel文件的導(dǎo)入和導(dǎo)出成為了一項(xiàng)常見而重要的任務(wù),本文主要介紹了如何在熟悉的電子表格?UI?中輕松導(dǎo)入?Excel?文件,需要的可以參考下2024-03-03
javascript獲取文檔坐標(biāo)和視口坐標(biāo)
制作網(wǎng)頁的過程中,你有時(shí)候需要知道某個(gè)元素在網(wǎng)頁上的確切位置。下面的教程總結(jié)了Javascript在網(wǎng)頁定位方面的相關(guān)知識(shí)。有需要的小伙伴可以參考下。2015-05-05
javascritp添加url參數(shù)將參數(shù)加入到url中
javascritp添加url參數(shù)方法,將參數(shù)加入到url中,如果原來url中有則覆蓋,下面是示例代碼,感興趣的朋友可以參考下2014-09-09
初學(xué)js插入節(jié)點(diǎn)appendChild insertBefore使用方法
由于可見insertBefore()方法的特性是在已有的子節(jié)點(diǎn)前面插入新的節(jié)點(diǎn)但是兩種情況結(jié)合起來發(fā)現(xiàn)insertBefore()方法插入節(jié)點(diǎn),是可以在子節(jié)點(diǎn)列表的任意位置。2011-07-07
用javascript做一個(gè)webgame連連看大家看下
2008-01-01

