计时器数据开始下周

时间:2017-07-11 07:24:20

标签: javascript timer

function countDown(){
  // Set the date we're counting down to
  var countDownDate = new Date("july 11, 2017 10:19:00").getTime();

  // Update the count down every 1 second
  var x = setInterval(function() {

      // Get todays date and time
      var now = new Date().getTime();

      // Find the distance between now an the count down date
      var distance = countDownDate - now;

      // Time calculations for days, hours, minutes and seconds
      var days = Math.floor(distance / (1000 * 60 * 60 * 24));
      var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
      var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
      var seconds = Math.floor((distance % (1000 * 60)) / 1000);

      // Output the result in an element with id="demo"
      document.getElementById("demo").innerHTML = days + "d " + hours + "h "
      + minutes + "m " + seconds + "s ";

      // If the count down is over, write some text 
      if (distance < 0) {
          countDownDate = new Date("july 18, 2017 10:19:00").getTime();
      }
  }, 1000);

}

countDown();

我想要达到的是计时器将开始计入下周的每周。 我有一些东西在同一天和同一时间每周开始。

我不想每周重写代码:/

谢谢。

2 个答案:

答案 0 :(得分:0)

我以为你想在10:19:00到下周二每个星期二倒计时。 我现在懒得测试每一个案子,我觉得它应该有效。

function getNextTuesday() {
  // Get the date from now
  var date = new Date();

  // Set target hour/minute/seconds
  date.setHours(10);
  date.setMinutes(19);
  date.setSeconds(0);

  // Seek for the next tuesday
  var actualDay = date.getDay();
  var targetDay = 2; //Tuesday

  // diff will give us the day span between today and the next tuesday
  var diff = targetDay - actualDay;
  
  // If the diff is less than 0 (we're sunday or monday, or we fall on the exact day, minutes after the target hour) then add a week
  if (diff < 0 || (date.getTime() - new Date().getTime()) <= 0) {
    diff += 7;
  }

  // Finally add the day span to the current date
  date.setDate(date.getDate() + diff);

  return date;
}

function countDown() {
  // Set the date we're counting down to
  var countDownDate = getNextTuesday();

  // Update the count down every 1 second
  var x = setInterval(function() {

    // Get todays date and time
    var now = new Date().getTime();

    // Find the distance between now an the count down date
    var distance = countDownDate - now;

    // Time calculations for days, hours, minutes and seconds
    var days = Math.floor(distance / (1000 * 60 * 60 * 24));
    var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
    var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
    var seconds = Math.floor((distance % (1000 * 60)) / 1000);

    if (distance < 0) {
      // If the count down is over, write some text 
      document.getElementById("demo").innerHTML = 'IT\'S HAPPENING !';
      countDownDate = getNextTuesday();
    } else {
      // Output the result in an element with id="demo"
      document.getElementById("demo").innerHTML = days + "d " + hours + "h " + minutes + "m " + seconds + "s ";
    }
  }, 1000);

}

countDown();
<div id="demo"></div>

答案 1 :(得分:0)

那么为什么需要这么多的计算来计算下周开始的剩余时间呢?例如,我正在编写一个示例代码来计算它。

function distranceToNextWeekStartInSeconds() {
    var now = new Date()
    var dayDiff = 7 - now.getDay();
    var startOfNextWeek = new Date(now.valueOf());
    startOfNextWeek.setDate(now.getDate() + dayDiff);
    startOfNextWeek.setHours(0);
    startOfNextWeek.setMinutes(0);
    startOfNextWeek.setSeconds(0);
    return Math.floor((startOfNextWeek - now) / 1000);
}

console.log('Seconds remaining to next week start: ' + distranceToNextWeekStartInSeconds())

你只需在你的计时器内调用这个函数进行实时计算和显示目的,就是这样。