当时间改变时,jQuery倒数计时器会打破周末时间

时间:2016-03-07 16:19:38

标签: jquery timer countdown jquery-countdown

我在博客文章中找到了这段代码,并使用http://keith-wood.name/countdown.html重新发布了24小时倒数计时器。我使用它的目的是作为第二天交付的截止日期,所以当下午4点的截止日期过后,计时器重置。第二天不在周末发货,所以他们的计算方法是在周五,周六或周日调整计时器。

这个JsFiddle显示了一个演示,除了周末外,它每天都计到下午4点:https://jsfiddle.net/u3t2n9jk/6/

我的问题

我只需要将截止时间改为下午4点到下午3点。如果由于某种原因我将var deadline = 16更改为15(24小时制)会打破星期五,星期六和星期日的计算。我还需要做些什么才能使其正常工作?

function nextDay() {
  var target = new Date();
  var deadline = 16; // Set 4pm/16:00hrs dispatch deadline

  if (target.getHours() < deadline || (target.getHours() == 15 && target.getMinutes() == 59 ) ) {

      console.log('Before deadline ' + target.getMinutes());

      if ( target.getDay() == 5 && target.getHours() == 16  && target.getSeconds() > 1 ) { // For Friday After 4PM
        target.setHours(88, 0, 0, 0);
      }
      else if ( target.getDay() == 6  ) { // For Saturday
        target.setHours(64, 0, 0, 0);
      }
      else if ( target.getDay() == 7  ) { // For Sunday
        target.setHours(30, 0, 0, 0);
      }
      else {
        target.setHours(deadline, 0, 0, 0);
      }

    } else if (target.getHours() > deadline  || (target.getHours() == deadline && target.getMinutes() >= 0)   )  {
         console.log('After deadline ' + target.getMinutes());

        if ( target.getDay() == 5 && target.getHours() == 16  && target.getSeconds() > 1 ) { // For Friday After 4PM
                 target.setDate(target.getDate() + 1);
                 target.setHours(88, 0, 0, 0);
              }
              else if ( target.getDay() == 6  ) { // For Saturday
                 target.setDate(target.getDate() + 1);
                 target.setHours(64, 0, 0, 0);
              }
              else if ( target.getDay() == 7  ) { // For Sunday
                 target.setDate(target.getDate() + 1);
                 target.setHours(30, 0, 0, 0);
              }

              else {
                 target.setDate(target.getDate() + 1);
         target.setHours(deadline, 0, 0, 0);
              }

    }

  return target;
}

1 个答案:

答案 0 :(得分:0)

在工作同事的帮助下,重新写下它背后的逻辑。请注意,您可以复制/粘贴此代码以使其正常工作,但您也需要jQuery Countdown plugin

function nextDay() {
var target = new Date();
var hours_now = target.getHours();
target.setHours(15);
target.setMinutes(0);
target.setSeconds(0);

// target is 3 today

if ( target.getDay() == 5 && hours_now >= 15 ) { // For Friday After 3PM
    target.setDate(target.getDate() + 3); // target is 3 on Monday
}
else if ( target.getDay() == 6  ) { // For Saturday
    target.setDate(target.getDate() + 2); // target is 3 on Monday
}
else if ( target.getDay() == 0 ) { // For Sunday
    target.setDate(target.getDate() + 1); // target is 3 on Monday
}
else if (hours_now >= 15){ // Any other day and after deadline
    target.setDate(target.getDate() + 1); // target is 3 Tomorrow
}
return target;
}

function reset() {
window.location.reload();
}

$('#timer').countdown({
until: nextDay(),
onExpiry: reset,
expiryText: '<div class="end">You have missed todays dispatch.</div>',
labels: ['Years', 'Months', 'Weeks', 'days', 'hrs', 'min', 's'],
labels1: ['Year', 'Month', 'Week', 'day', 'hr', 'min', 's'],
format: 'HMS'
});