2017-10-17 107 views
0

我有一些倒計時的代碼,但想暫停幾個小時,在0(顯示文本),然後再開始14天。倒計時JS再次開始

<script type="text/JavaScript"> 
var Display=document.getElementById("Counter"); 
function Countdown() { 
var date1 = new Date(); 
var date2 = new Date ("Oct 20 20:00:00 2017"); 
var sec = (date2 - date1)/1000; 
var n = 24 * 3600; 
if (sec > 0) { 
j = Math.floor (sec/n); 
h = Math.floor ((sec - (d * n))/3600); 
mn = Math.floor ((sec - ((d * n + h * 3600)))/60); 
sec = Math.floor (sec - ((d * n + h * 3600 + mn * 60))); 
Affiche.innerHTML = "Next game in : " + d +" d "+ h +" h "+ mn +" min "+ sec + " s "; 
window.status = "Remaining time : " + d +" d "+ h +" h "+ mn +" min "+ sec + " s "; 
} 
tCountdown=setTimeout ("Countdown();", 1000); 
} 
Countdown(); 
</script> 

所以總結一下:1, 倒計時達到0 2.塊4小時,並顯示一個文本(「我們目前正在玩」) 3.再次啓動了14天左右。

我想到的是這樣的事情再次啓動倒計時: VAR德恩= VAR日期2 +(的時間長度約14天)

我說得對不對? 我只能用Javascript做到這一點嗎?

+1

這似乎就像一個巨大的殺傷力,你真的認爲有人會在你的網站上那麼長時間看到區別嗎? :) – Icepickle

+0

誰sais這是一個網站?可能是屏幕上的某些東西等... –

回答

0

我把它分解成一堆函數,所以你可以推理它。如果你想測試它,你可以在sec變量的東西不多設置初始秒,像10,然後你還可以在setTimeout設置的第二個參數小東西,像10

<div id="counter"></div> 
<script> 
// initialize 
var first_target_date = new Date ("Oct 20 20:00:00 2017"); 
var sec = calcSecDiff(new Date(), first_target_date); 
var counter = document.getElementById("counter"); 
var timeout; // we will update this global variable when we want to stop the whole thing 

// start the countdown 
countdown(); 

// do it again every second 
var interval = setInterval(function(){ 
    countdown(); 
}, 1000); 


function countdown() { 
    counter.innerHTML = parseTime(sec); 
    // decrement the second 
    sec--; 

    // if we get to 0 
    if (sec < 0) { 

     clearInterval(interval); 

     counter.innerHTML = "We are currently playing"; 

     if (timeout) return; // it's over 

     var timeout = setTimeout(function(){ 

      sec = daysToSec(14); // reset the seconds to 14 days away 
      var interval = setInterval(function(){ 
       countdown(); 
      }, 1000); 

     }, hrsToMs(4)); // wait four hours before counting down again 
    }; 
} 

// returns days, hours, minutes, and seconds from seconds 
// see https://stackoverflow.com/questions/13903897/javascript-return-number-of-days-hours-minutes-seconds-between-two-dates 
function parseTime(sec){ 

    // calculate (and subtract) whole days 
    var days = Math.floor(sec/86400); 
    sec -= days * 86400; 

    // calculate (and subtract) whole hours 
    var hours = Math.floor(sec/3600) % 24; 
    sec -= hours * 3600; 

    // calculate (and subtract) whole minutes 
    var minutes = Math.floor(sec/60) % 60; 
    sec -= minutes * 60; 

    // what's left is seconds 
    var seconds = sec % 60; 

    return days + " days, " + hours + " hours, " + minutes + " minutes, " + seconds + " seconds"; 
} 

// calculates the difference between two dates in seconds 
function calcSecDiff(date1, date2){ 
    return Math.round((date2 - date1)/1000); 
} 

// converts hours to milliseconds 
function hrsToMs(hrs){ 
    return hrs * 60 * 60 * 1000; 
} 

// converts days to seconds 
function daysToSec(days){ 
    return days * 24 * 60 * 60; 
} 
</script> 
+0

謝謝,真的很有用。 它正在工作,但我沒有找到「hrsToMs(4))」在哪裏被觸發? 我需要自己完成一些工作來完成代碼嗎? – Greg

+0

hrsToMs只是將小時數轉換爲毫秒數,這是setTimeout()的第二個參數。 https://www.w3schools.com/jsref/met_win_settimeout.asp –