JS實現倒計時(天數、時、分、秒)


 

<html>
<body>
<input type="text" value="" id="date2">
<input type="button" onclick="go(document.getElementById('date2').value);" value="開始">
<div id="timer">fdd</div>
<script type="text/javascript">
 // 倒計時
 var _ordertimer = null;
 var data=new Date();
 document.getElementById("date2").value=data.getFullYear()+'-'+(data.getMonth()+1)+'-'+data.getDate()+' '+data.getHours()+':'+data.getMinutes()+':'+data.getSeconds();//當前時間
 function leftTimer(enddate) {
  var leftTime = (new Date(enddate)) - new Date(); //計算剩余的毫秒數
  var days = parseInt(leftTime / 1000 / 60 / 60 / 24, 10); //計算剩余的天數
  var hours = parseInt(leftTime / 1000 / 60 / 60 % 24, 10); //計算剩余的小時
  var minutes = parseInt(leftTime / 1000 / 60 % 60, 10);//計算剩余的分鍾
  var seconds = parseInt(leftTime / 1000 % 60, 10);//計算剩余的秒數
  days = checkTime(days);
  hours = checkTime(hours);
  minutes = checkTime(minutes);
  seconds = checkTime(seconds);
  
if(days > 0 && hours >= 0 && minutes >= 0 && seconds >= 0){
console.log(1111);
$(ele).html(days + " " + hours + ":" + minutes + ":" + seconds);
}
else if(days==0 && !(hours <= 0 && minutes <= 0 && seconds <= 0)){
console.log(2222);
$(ele).html(hours + ":" + minutes + ":" + seconds);
}
else{
console.log(3333);
$(ele).html('已結束');
window.clearInterval(_ordertimer);
_ordertimer = null;
}
} function checkTime(i) { //將0-9的數字前面加上0,例1變為01 if (i < 10) { i = "0" + i; } return i; } function go(v){ var date1=new Date(),data2=new Date(v); if(data2<date1)return;//設置的時間小於現在時間退出  _ordertimer = setInterval(function(){leftTimer(data2)}, 1000); } </script> </body> </html>

https://www.jb51.net/article/97501.htm

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM