简体   繁体   English

Javascript倒计时不会在每个星期六重复

[英]Javascript countdown not repeating every saturday

my countdown should repeat every saturday at 8pm (gmt+2), however once it hits 8pm, the countdown is stuck on EXPIRED. 我的倒数应该在每个星期六的晚上8点(gmt + 2)重复,但是一旦到达晚上8点,倒数就会停留在EXPIRED上。 I don't understand why, does someone recognize the issue? 我不明白为什么,有人知道这个问题吗?

Appreciate any help and thanks in advance. 提前感谢您的帮助和感谢。

 function nextSaturday() {
    var d = new Date();
    console.log(d.getDay());
    if (d.getDay() == 7 && d.getHours() < 20){
      d.setHours(20);
      d.setMinutes(0);
      d.setSeconds(0);
      return d;
    }
    switch (d.getDay()) {
        case 0: d.setDate(d.getDate() + 6);
            break;
        case 1: d.setDate(d.getDate() + 5);
            break;
        case 2: d.setDate(d.getDate() + 4);
            break;
        case 3: d.setDate(d.getDate() + 3);
            break;
        case 4: d.setDate(d.getDate() + 2);
            break;
        case 5: d.setDate(d.getDate() + 1);
            break;
        case 6: d.setDate(d.getDate() + 0);
            break;
    }
    d.setHours(20);
    d.setMinutes(0);
    d.setSeconds(0);
    return d;
}

var end = nextSaturday();
var _second = 1000;
var _minute = _second * 60;
var _hour = _minute * 60;
var _day = _hour * 24;
var timer;

function showRemaining() {
    var now = new Date();
    var distance = end - now;
    if (distance < 0) {

        clearInterval(timer);
        document.getElementById("countdown").innerHTML = "EXPIRED!";

        return;
    }
    var days = Math.floor(distance / _day);
    var hours = Math.floor((distance % _day) / _hour);
    var minutes = Math.floor((distance % _hour) / _minute);
    var seconds = Math.floor((distance % _minute) / _second);

    document.getElementById("countdown").innerHTML = "Countdown ends in: ";
    document.getElementById("countdown").innerHTML += days + " days ";
    document.getElementById("countdown").innerHTML += hours + " hours ";
    document.getElementById("countdown").innerHTML += minutes + " minutes and ";
    document.getElementById("countdown").innerHTML += seconds + " seconds left";

}
showRemaining();
timer = setInterval(showRemaining, 1000);

You are returning from this function early. 您将尽早从此功能返回。

if(d.getDay() == 7 && d.getHours() < 20) { 
  d.setHours(20);
  d.setMinutes(0);
  d.setSeconds(0);
  return d; //right here you are exiting your loop at 8:00 (20 hours)
}

return d; terminates execution of the function and returns the value of d 终止函数的执行并返回d的值

You have d set to this Saturday by this point, you want it to be next Saturday, so you need to add this: 你已经d这个点设置到这个星期六,你希望它是下周六,所以你需要补充一点:

d.setDate(d.getDate() + 7);

before you return d . 在您返回d之前。

Explanation: 说明:

That if statement above is basically saying, "If it's after 8:00pm on Saturday, set d to 8:00pm on Saturday." 上面的if语句基本上是说:“如果在星期六晚上8:00之后,请将d设置为星期六晚上8:00”。 Your code will start working again at 12:00am on Sunday when it starts resetting the date to the next Saturday, you simply forgot to increment the date if it happens to be Saturday like you do for all the other days of the week. 当代码开始将日期重置为下一个星期六时,您的代码将在星期日的12:00 am重新开始工作,如果您恰好像在一周中所有其他天一样都是星期六,则您只是忘记了增加日期。

The code exactly as it should be: 该代码完全应该是:

if(d.getDay() == 7) { //note: I've also updated this line to account for changes in hours
  d.setHours(20);
  d.setMinutes(0);
  d.setSeconds(0);
    if(d.getHours() >= 20)
      d.setDate(d.getDate() + 7);
  return d;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM