相对于当前时间创建javascript倒计时器

时间:2015-06-16 12:29:51

标签: javascript timer

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

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

我已经能够创建一个每小时倒计时,但这只适用于一个计时器,而我无法弄清楚如何超过一个小时......

这是我当前的代码,每秒由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

任何帮助都将不胜感激。

谢谢:)

1 个答案:

答案 0 :(得分:0)

这是你需要的吗?

<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>