JavaScript定時器實現限時秒殺功能

本文實例為大傢分享瞭JavaScript實現限時秒殺功能的具體代碼,供大傢參考,具體內容如下

文件index.html

代碼:

<!DOCTYPE html>
<html>
 <head>
  <meta charset="GBK" />
  <title>show</title>
  <link rel="stylesheet" href="css/index.css" type="text/css" />
  
 </head>
 <body>
  
  <div class="divMain">
   <img src="img/secondBuyImg.jpg" id="imgMain" />
   <h3 style="color: #FF0000;">距離本廠秒殺結束還剩:</h3>
   <div id="divSecond">
    
    <div id="divF1" class="divFours">
     <font class="fontdate" size="4" id="font1">00</font>
     <font class="fonttext" size="4">天</font>
    </div>
    <div id="divF2" class="divFours">
     <font class="fontdate" size="4" id="font2">00</font>
     <font class="fonttext" size="4">時</font>
    </div>
    <div id="divF3" class="divFours">
     <font class="fontdate" size="4" id="font3">00</font>
     <font class="fonttext" size="4">分</font>
    </div>
    <div id="divF4" class="divFours">
     <font class="fontdate" size="4" id="font4">00</font>
     <font class="fonttext" size="4">秒</font>
    </div>
   </div>
  </div>
  
 </body>
</html>
<script type="text/javascript" src="js/index.js"></script>

樣式表文件index.css

#imgMain{
 
}
.divMain{/*外層div*/
width: 100%;
height: 100%;
 display: flex;
 justify-content: left;
 align-items: center;/*位於彈性盒子垂直方向的中心*/
 flex-direction: column;/*設置彈性盒子排序方向*/
}
/* 外層時間div */
#divSecond{
 width: 500px;
 height: 500px;
 display: flex;
 justify-content: center;
 align-items: flex-start;
 flex-direction: row;
}
.divFours{/*時間div1-4*/
 width: 10%;
 height: 10%;
 border: 2px solid red;
 display: flex;
 flex-direction: row;
 justify-content: center;
 align-items: center;
}
/* 圖片外層div */
#divFrist{
 width: 20%;
}
/* 剩餘時間顯示 */
.fontdate{
 color: deeppink;
}
.fonttext{
 color: darkblue;
}

JavaScript文件index.js

function torun(str){
 
 var date=new Date();
 var systemDay=date.getDate();
 var systemHour=date.getHours();
 var systemMinute=date.getMinutes();
 var systemSecond=date.getSeconds();
 
 var endDate=new Date(str);
 var endDay=endDate.getDate();
 var endHour=endDate.getHours();
 var endMinute=endDate.getMinutes();
 var endSecond=endDate.getSeconds();
 
 var remainingDay=-(systemDay-endDay)-1;
 var remainingHour=-(systemHour-endHour)-1;
 var remainingMinute=-(systemMinute-endMinute)-1;
 var remainingSecond=(systemSecond-endSecond>=0)?systemSecond-endSecond+59:-(systemSecond-endSecond)-1;
 
 
 console.log("剩餘"+remainingDay+"天");
 console.log("剩餘"+remainingHour+"時");
 console.log("剩餘"+remainingMinute+"分");
 console.log("剩餘"+remainingSecond+"秒");
 
 console.log("當前天"+systemDay+"結束天"+endDay);
 console.log("當前時"+systemHour+"結束時"+endHour);
 console.log("當前分"+systemMinute+"結束分"+endMinute);
 console.log("當前秒"+systemSecond+"結束秒"+endSecond);
 
 document.getElementById("font1").innerText=remainingDay+"";
 document.getElementById("font2").innerText=remainingHour+"";
 document.getElementById("font3").innerText=remainingMinute+"";
 document.getElementById("font4").innerText=remainingSecond+"";
 
}
var int=setInterval('torun("2020-12-5 23:59:59")',60);

目錄

效果

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: