2017-10-17 101 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 –