创建相对于当前时间的 JavaScript 倒数计时器

Creating javascript countdown timer relative to the current time

本文关键字:JavaScript 倒数 计时器 时间 相对于 创建      更新时间:2023-09-26

所以我不会创建一个倒计时到特定小时的计时器。

例如;我可能有 6 个计时器同时运行,每个计时器倒计时到特定小时(第一个倒计时到中午 12:00,第二个倒计时到下午 1:00,第三个倒计时到下午 2:00 等)。

我已经能够创建一个每小时倒计时,但这仅适用于一个计时器,而且我不知道如何超过一个小时......

这是我当前的代码,每秒由setInterval执行一次:

time  = new Date
mins  = do time.getMinutes
mins  = ( 59 - mins ) % 60
secs  = do time.getSeconds
if secs != 60
    secs = ( 59 - secs ) % 60
else
    secs = 0
time = [ Number( mins ), Number( secs ) ]
minutes = time[1]
seconds = time[2]
minutes = '0' + time[1] if time[1] < 10
seconds = '0' + time[2] if time[2] < 10
console.log minutes, seconds

任何帮助将不胜感激。

谢谢 : )

这是你需要的吗?

<html>
<head>
</head>
<body>
 <h1 id="header">Click on the following button to start counting for 6 hours, your will get a notification on your console window after 6 hours.</h1>
 <br>
 <input type="button" style="font-weight:bold;cursor:pointer;" value="Click Me to start Counting" onclick="
indicate(21600000);">
<script type="text/javascript">
   function indicate(milliseconds){
    var current = Date.now();
    var Date_future = current + milliseconds;
    var period_done = "not_done";
    console.log("Status: Count Started");
    while (Date.now()!==Date_future) {
      if (Date.now()==Date_future){
        period_done = "done";
      }
    } 
    console.log("Status: Count Stopped, 6 hours finished");
   }
</script>
</body>
</html>