<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>js test</title>
<script>
/*
時間倒計時插件
TimeDown.js
*/
function TimeDown(id, endDateStr,times) {
//結束時間
var endDate = new Date(endDateStr);
//當前時間
var nowDate = new Date();
//相差的總秒數
var totalSeconds = parseInt((endDate - nowDate) / 1000);
//天數
var days = Math.floor(totalSeconds / (60 * 60 * 24));
//取模(余數)
var modulo = totalSeconds % (60 * 60 * 24);
//小時數
var hours = Math.floor(modulo / (60 * 60));
modulo = modulo % (60 * 60);
//分鍾
var minutes = Math.floor(modulo / 60);
//秒
var seconds = modulo % 60;
//輸出到頁面
document.getElementById(id).innerHTML = "還剩:" + days + "天" + hours + "小時" + minutes + "分鍾" + seconds + "秒";
//延遲一秒執行自己
if(times>minutes && minutes!=0){
times--;
//修改數據庫時間
console.log("寫入數據庫數據:" + times);
}
setTimeout(function () {
//15分鍾結束后暫停倒計時
if(minutes>=0){
TimeDown(id,endDateStr,times);
}
}, 1000)
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div id="show">
</div>
<script type="text/javascript">
var times = 15;
//獲取當前時間
var date = new Date();
//修改分鍾
date.setMinutes(date.getMinutes()+times);
TimeDown("show",date,times);
</script>
</form>
</body>
</html>