倒計時:
1.設定一個有效的結束日期
2.計算剩餘時間
3.将時間轉換成可用的格式
4.輸出時鐘資料作為一個可重用的對象
5.在頁面上顯示時鐘,并在它到達0時停止
html
天
時
分
秒
js代碼
function getTimeReamin(endtime){
//剩餘的秒數
var remainSec=(Date.parse(endtime)-Date.parse(new Date()))/1000;
var days=Math.floor(remainSec/(3600*24));
var hours=Math.floor(remainSec/3600%24);
var minutes=Math.floor(remainSec/60%60);
var seconds=Math.floor(remainSec%60);
return{"remainSec":remainSec,
"days":days,
"hours":hours,
"minutes":minutes,
"seconds":seconds
}
}
var endtime="2016/10/10";
var clock=document.getElementById("clock");
//設定定時器
var timeid=setInterval(function () {
var t=getTimeReamin(endtime);
//判斷時間格式
days= t.days>=0&& t.days<10?"0"+t.days:t.days;
hours= t.hours>=0&& t.hours<10?"0"+t.hours:t.hours;
minutes=t.minutes>=0&&t.minutes<10?"0"+t.minutes:t.minutes;
seconds=t.seconds>=0&&t.seconds<10?"0"+t.seconds:t.seconds;
//設定時間
document.getElementById("days").innerText= days;
document.getElementById("hours").innerText= hours;
document.getElementById("minutes").innerText= minutes;
document.getElementById("seconds").innerText=seconds;
//清除定時器
if(t.remainSec<=0){
clearInterval(timeid);
}
});
以上就是本文的全部内容,希望對大家的學習有所幫助,也希望大家多多支援腳本之家。