【问题标题】:How can I prevent this daily countdown timer from resetting at midnight?如何防止此每日倒计时计时器在午夜重置?
【发布时间】:2018-08-21 02:39:19
【问题描述】:

要记住的关键变量:

设置为模拟晚上 11:00。 var bt = "23:00";
这设置为模拟上午 8:00。 var wt = "08:00";

所需的功能:

  • 每天早上 8:00 开始倒计时。
  • 每晚倒计时到晚上 11:00。
  • 然后它停留在 00:00:00。
  • 早上 8:00,它再次重复倒计时。
  • 这应该永远发生。

实际情况:

  • 一切正常,除了在午夜开始 24 小时倒计时,一直到上午 8:00。

我试过调试这个,我怀疑错误出在distance变量的计算上,使代码认为它正在与第二天进行比较,但我不知道如何解决这个问题。

这里是Codepen

这是我的 JS 代码:

$(document).ready(function () {

    var bt = "23:00";  // 11:00 PM
    var wt = "08:00";  // 08:00 AM

    var dateNow = moment().format('MMM D, YYYY');
    placeHolderDate = dateNow + " " + bt;

    var timeNow = moment().format('HH:mm');

    var countDownDate = new Date(placeHolderDate).getTime();

    var countDownHourMin = (wt.split(":"));


// 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 and the count down date
        var distance = countDownDate - now;

        // Time calculations for days, hours, minutes and seconds
        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);

        $("#countDown").val(hours + ":" + minutes + ":" + seconds);

        // If the countdown is over, write some text
        if (hours === 0 && minutes === 0 && seconds === 0) {
            //clearInterval(x);
            $("#countDown").val("00:00:00");
        }

        if (hours < 0 || minutes < 0 || seconds < 0) {
            //clearInterval(x);
            $("#countDown").val("00:00:00");
        }
        var timeNow = moment().format('HH:mm');
        //console.log('Time Now:' + timeNow);
        //console.log('Wake Time:' + wt);
        if (timeNow === wt) {
            clearInterval(x);
            restartCountdown();
        }


        //console.log(hours + ":" + minutes + ":" + seconds);

    }, 1000);


    function restartCountdown() {
        //log("restartCountdown Started!");

    var bt = "23:00";  // 11:00 PM
    var wt = "08:00";  // 08:00 AM

        var dN = (moment().add(moment.duration({d: 1})).format('MMM D, YYYY'));
        console.log('dn ' + dN);

        var placeHolderDate = dN + " " + bt;
        var countDownDate = new Date(placeHolderDate).getTime();

        var countDownHourMin = (wt.split(":"));


        // 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 and the count down date
            var distance = countDownDate - now;

            // Time calculations for days, hours, minutes and seconds
            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);

            $("#countDown").val(hours + ":" + minutes + ":" + seconds);

            // If the countdown is over, write some text
            if (hours === 0 && minutes === 0 && seconds === 0) {
                //clearInterval(x);
                $("#countDown").val("00:00:00");
            }

            if (hours < 0 || minutes < 0 || seconds < 0) {
                //clearInterval(x);
                $("#countDown").val("00:00:00");
            }

            //  console.log(hours + ":" + minutes + ":" + seconds);

        }, 1000);

    }
});

【问题讨论】:

  • 如果我没记错的话,您希望它在23:00 - 08:00 之间停止。哪些线路可以完成这项工作?这与您的if (timeNow === wt) 矛盾,restartCountdown 会立即重新启动它。
  • @choz 正确。我希望它在 23:00 - 08:00 之间停止 - 或者至少在视觉上向用户显示 00:00:00。 if 语句实际上是在上午 08:00 正确启动它。另外,请注意wtbt在实际应用中是可以更改的,这里为了演示而进行了硬编码。

标签: javascript time momentjs countdown


【解决方案1】:

我已编辑您的代码并创建了此代码笔https://codepen.io/anon/pen/aabjEb?editors=0010。请随意优化代码,因为我还没有这样做。这个想法是检查时间是否在上午 8 点到晚上 11 点之间。如果是,则显示值,否则显示 00:00:00。同样,一旦日期更改,请更新日期并现在进行相应计算

$(document).ready(function () {

  function countdown() {
     var bt = "23:00",  // 11:00 PM
         wt = "08:00";  // 08:00 AM


    var today = new Date(),
        dd = today.getDate(),
        mm = today.getMonth()+1,
        yyyy = today.getFullYear();

    var startTime = new Date(mm + '/' + dd + '/' + yyyy + ' ' + wt),
        endTime = new Date(mm + '/' + dd + '/' + yyyy + ' ' + bt);

    setInterval(function() {
       var now = new Date();
       var nowdd = today.getDate();
       var nowTime = now.getTime();
      if(dd !== nowdd) {
        dd = nowdd;
        var nowmm = now.getMonth() + 1,
            nowyyyy = now.getFullYear();
        startTime = new Date(dd + '/' + mm + '/' + yyyy + ' wt');
        endTime = new Date(dd + '/' + mm + '/' + yyyy + ' bt');
      }

      if(nowTime > startTime && nowTime < endTime) {
         // Find the distance between now and the count down date
            var distance = endTime - nowTime;

            // Time calculations for days, hours, minutes and seconds
            var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)),
                minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60)),
                seconds = Math.floor((distance % (1000 * 60)) / 1000);
            $("#countDown").val(hours + ":" + minutes + ":" + seconds);
         } else {
           $("#countDown").val("00:00:00");
         }
    }, 1000);
  }
  countdown();
});

【讨论】:

  • 这似乎有一个错误。尝试这些时间:bt = "02:30"wt = "09:00"
  • 根据逻辑和代码。 wt 必须是 2:30,bt 必须是 9:00
  • 我尝试了 2:30 和 9:00,但结果相同。例如,现在的时间是我所在的晚上 11:22。我将 bt 设置为 2:00 (AM) 并将 wt 设置为 9:00 (AM) 但计数器仍显示 00:00:00
  • 好的,这意味着第 1 天的上午 9:00 和第 2 天的凌晨 2 点。在这种情况下,您需要在 endTime 中添加一天
  • 我已经编辑了 codepen,因此您没有提供结束时间,而是提到了您希望计数器运行的开始时间后的小时数。这将在一天发生变化时照顾到这一事实
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多