JS倒計時代碼

<!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>
相關文章
相關標籤/搜索