JS實現(xiàn)根據(jù)用戶輸入分鐘進(jìn)行倒計時功能
廢話不多說了,直接給大家貼代碼了。具體代碼如下所示:
其實這倒計時之前有接觸過很多,只是用的都是別人的源碼。
應(yīng)項目需求,終于認(rèn)真一回,把一個自己看似很簡單的問題,終于耗上了跨度一個星期的時間,才弄出來。
源碼直接復(fù)制黏貼可用。
冗余版+簡化版。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
</head> <body>
<script type="text/javascript">
var createTime = '2016-11-14 10:20:00';//開始時間
var limitTimes = 10;//時間長度
// 倒計時 入口
countdowns = window.setInterval(function(){
var arr = cutDoowns(limitTimes,createTime);
document.write(formatDate(arr[0])+':'+formatDate(arr[1])+':'+formatDate(arr[2])+'</br>');
if(arr[2]){
document.write('時間到!');
}
},1000);
/*
s,10分鐘后的具體日期:
date,開始時間
然后轉(zhuǎn)化成毫秒比較,所得的差值化成分秒,就是倒計時的分秒。
*/
function cutDoowns(s,date){
console.log('');
var flag = false;
var arr = [];//arr[0]:分,arr[1]:秒,arr[2]:返回boolean
var now = new Date();//當(dāng)前時間
var now1 = new Date(date);//開始時間
console.log('開始時間 now1: '+now1);
now1.setMinutes(now1.getMinutes()+s);//10分鐘后的時間
console.log('當(dāng)前時間 now :'+now);
console.log('10分鐘時 now1:'+now1);
// 轉(zhuǎn)化成年月日 時分秒 格式
var n = now.getFullYear()+'/'+(now.getMonth()+1)+'/'+now.getDay()+' '+now.getHours()+':'+now.getMinutes()+':'+now.getSeconds();
var n1 = now1.getFullYear()+'/'+(now1.getMonth()+1)+'/'+now1.getDay()+' '+now1.getHours()+':'+now1.getMinutes()+':'+now1.getSeconds();
// 日期轉(zhuǎn)化成毫秒
var time1 = (new Date(n)).getTime();
var time2 = (new Date(n1)).getTime();
// 毫秒轉(zhuǎn)日期格式
var time11 = new Date(time1);
var time21 = new Date(time2);
console.log('當(dāng)前時間:'+n+',轉(zhuǎn)化成毫秒:'+time1+',time11:'+time11);
console.log('10分鐘時:'+n1+',轉(zhuǎn)化成毫秒:'+time2+',time21:'+time21);
var surplusSec = time2-time1;//距離解鎖剩余毫秒
if(surplusSec<=0){
clearInterval(countdowns);
flag = true;
return arr = [00,00,flag];
}
var minute = Math.floor(surplusSec/1000/60);//分鐘
var second = Math.floor((surplusSec-minute*60*1000)/1000);//剩余秒數(shù)
console.log('剩余時間,minute: '+minute+',second: '+second+',surplusSec:'+surplusSec);
// var second = Math.round(surplusTimes);//秒數(shù)
console.log('剩余時間,minute: '+minute+',second: '+second+',surplusSec:'+surplusSec);
arr = [minute,second,flag];
return arr;
}
//格式化日期:把單字符轉(zhuǎn)成雙字符
function formatDate(n){
n = n.toString();
// console.log(n);
if(n.length<=1){
n = '0'+n;
}
// console.log(n);
return n;
}
</script>
</body>
</html>
簡化版本:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>打開調(diào)試工具,看效果!</title>
</head>
<body>
<script type="text/javascript">
/*
打開調(diào)試工具,看效果!
思路:
1.設(shè)置一個倒計時的時間長度;
2.設(shè)置開始時間和當(dāng)前時間;
3.結(jié)束時間是 開始時間+倒計時間;
4.結(jié)束毫秒-開始毫秒=剩余倒計時間。
*/
// 準(zhǔn)備
var countdownMinute = 10;//10分鐘倒計時
var startTimes = new Date('2016-11-16 15:55');//開始時間 new Date('2016-11-16 15:21');
var endTimes = new Date(startTimes.setMinutes(startTimes.getMinutes()+countdownMinute));//結(jié)束時間
var curTimes = new Date();//當(dāng)前時間
var surplusTimes = endTimes.getTime()/1000 - curTimes.getTime()/1000;//結(jié)束毫秒-開始毫秒=剩余倒計時間
// 進(jìn)入倒計時
countdowns = window.setInterval(function(){
surplusTimes--;
var minu = Math.floor(surplusTimes/60);
var secd = Math.round(surplusTimes%60);
console.log(minu+':'+secd);
if(surplusTimes<=0){
console.log('時間到!');
clearInterval(countdowns);
}
},1000);
</script>
</body>
</html>
以上所述是小編給大家介紹的JS實現(xiàn)根據(jù)用戶輸入分鐘進(jìn)行倒計時功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
DropDownList控件綁定數(shù)據(jù)源的三種方法
本文給大家分享web 中 DropDownList綁定數(shù)據(jù)源的幾種方式以及DropdownList控件動態(tài)綁定數(shù)據(jù)源的兩種方法,下面通過本文給大家詳細(xì)介紹,感興趣的朋友一起看看2016-12-12
Layui table field初始化加載時進(jìn)行隱藏的方法
今天小編就為大家分享一篇Layui table field初始化加載時進(jìn)行隱藏的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-09-09
js編寫trim()函數(shù)及正則表達(dá)式的運用
js中本身是沒有trim函數(shù)的,不過你可以自己寫一個,下面的實現(xiàn)方法是用到了正則表達(dá)式,效率不錯,并把這三個方法加入String對象的內(nèi)置方法中去2013-10-10
JavaScript字符串String和Array操作的有趣方法
字符串和數(shù)組在程序編寫過程中是十分常用的類型,因此程序語言都會將String和Array作為基本類型,并提供許多字符串和數(shù)組的方法來簡化對字符串的操作2012-12-12
js實現(xiàn)拖拽 閉包函數(shù)詳細(xì)介紹
在開發(fā)過程中可能會使用js實現(xiàn)拖拽等相關(guān)功能,本文將以此問題進(jìn)行深入介紹,需要了解的朋友可以參考下2012-11-11

