【问题标题】:Check if the time distance is between different times检查时间距离是否在不同时间之间
【发布时间】:2022-12-11 15:26:58
【问题描述】:

我从我的函数中使用 javascript 和 php 进行了倒计时,倒计时有效,但现在我想要 3 个选项:

  1. 如果倒计时超过 24 小时,请显示 te selector.next(".countdown").html(expiry); date

  2. 如果倒计时为 6 小时或更短时间,请显示计时器selector.next(".countdown").html(days + "d " + hours + "h " + minutes + "m " + seconds + "s ");

  3. 否则倒计时小于0表示endend selector.next(".countdown").html(<p>Closed</p>);

    $(".expiry").each(function() {
        var expiry = new Date($(this).text());
        var selector = $(this)
        var x = setInterval(function() {
            var currentDateObj = new Date();
            var numberOfMlSeconds = currentDateObj.getTime();
            var addMlSeconds = 60 * 60 * 1000;
            var now = new Date(numberOfMlSeconds - addMlSeconds);
    
            var distance = expiry - now;
            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 >= 86400000 && distance < 21600000){
              selector.next(".countdown").html(expiry);  
            }else if ( distance <= 21600000 && distance > 0){
              selector.next(".countdown").html(days + "d " + hours + "h " + minutes + "m " + seconds + "s "); 
            }else{
              selector.next(".countdown").html('<p>error</p>');
            }
        }, 1000);
    });
    

【问题讨论】:

  • 86400000永远不会小于21600000,条件永远不会通过。也许你在这里需要 || 而不是 &amp;&amp; ..?
  • 它现在总是显示错误 btw

标签: javascript html jquery


【解决方案1】:

您的 IF 语句的第一行是否正确?你想检查它是否在这两个数字之间吗?如果是这样,条件不应该是:

if(distance <= 86400000 && distance > 21600000)

IE。如果距离是小于或等于86400000 和比...更棒21600000

目前你正在检查 distance 是否是大于或等于 86400000小于 21600000这总是会产生错误

编辑 查看带有条件的完整 if 语句块大于 86400000

if (distance > 86400000) {
    selector.next(".countdown").html('<p>A LOOOOOONG Way off expiring</p>');            
}
else if (distance <= 864000000 && distance > 432000000) { 
    selector.next(".countdown").html(expiry); 
}
else if (distance <= 432000000 && distance > 0) {
    selector.next(".countdown").html(days + "d " + hours + "h " + minutes + "m " + seconds + "s "); 
}
else if (distance < 0) { 
    selector.next(".countdown").html('<p>afgelopen</p>'); 
}
else { 
    selector.next(".countdown").html('<p>Error</p>'); 
}

【讨论】:

  • 我得到了这个,但仍然出现错误 if( distance <= 864000000 && distance > 432000000 ){ selector.next(".countdown").html(expiry); }else if ( distance <= 432000000 && distance > 0 ){ selector.next(".countdown").html(days + "d " + hours + "h " + minutes + "m " + seconds + "s ") ; }else if ( distance < 0) { selector.next(".countdown").html('<p>afgelopen</p>'); }else{ selector.next(".countdown").html('<p>错误</p>'); }
  • @Bent 为清楚起见,当您说错误时,您指的是错误选择器还是页面上的实际错误
  • 就像错误选择器一样,if else 中的最后一个 else
  • @Bent 你有没有验证过的价值距离是否有机会验证您是否获得了您期望的距离值?将错误选择器更改为选择器.next(".countdown").html(距离);这将打印距离的值而不是错误这个词。你得到了什么?
  • 像 1202192152 这样的值 所以这应该是以毫秒为单位的日期之间的距离
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-25
  • 1970-01-01
  • 2015-06-16
  • 2017-05-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多