【问题标题】:How to make the countdown to every Sunday in the month?每月的每个星期天如何倒计时?
【发布时间】:2013-07-26 08:13:56
【问题描述】:

我的网站上有倒计时,我希望它倒计时到下周日上午 11:00,当它是周日上午 11:01 时,我希望它自动倒计时到下周日上午 11:00。我希望每个月都重复一次..

我有这个脚本,但似乎无法让它工作,有人可以帮我吗?

提前致谢

这是我的html...

<div class="eventtd">
        <div class="nextevt">Next Service Start In:</div>
           <div class="time" contents="2" rel="1374397200">
       </div> <!-- end time -->

这是我使用的脚本,截至目前,当它达到 0 时,它会继续变为负值而不是重置。

jQuery(document).ready(function($) {

if (timerhelp == 'yes') {
    var init = setInterval(animation, 100);
}
function animation(){       
    var deadline1 = $('.time').attr('rel');
    var deadline2 = $('.time').attr('contents');
    var now = new Date();
    now = Math.floor(now / 1000);
    now = now + Math.floor(deadline2 * 60 * 60);
    var counter1 = deadline1 - now;
    var seconds1=Math.floor(counter1 % 60);
    if (seconds1 < 10 && seconds1 > 0 ){
        seconds1 = '0'+seconds1;

    }
    counter1=counter1/60;
    var minutes1=Math.floor(counter1 % 60);
    if (minutes1 < 10 && minutes1 > 0){
        minutes1 = '0'+minutes1;

    }
    counter1=counter1/60;
    var hours1=Math.floor(counter1 % 24);
    if (hours1 < 10 && hours1 > 0){
        hours1 = '0'+hours1;
    }
    counter1=counter1/24;
    var days1=Math.floor(counter1);
    if (days1 < 10 && days1 > 0){
        days1 = '0'+days1;
    }
    $('.time').html('<table><tbody><tr><th class="day">'+days1+'</th><th class="day">'+hours1+'</th><th class="day">'+minutes1+'</th><th class="day">'+seconds1+'</th></tr><tr><th>Days</th><th>Hours</th><th>Min</th><th>Sec</th></tr></tbody></table>');


}
});

【问题讨论】:

    标签: javascript jquery counter countdown


    【解决方案1】:

    这段代码应该总是在下个星期天给你:

    var now = new Date();
    var sunday = new Date();
    sunday.setDate(now.getDate() - now.getDay()); // Make Sunday
    sunday.setHours(11); // Set 11am
    sunday.setMinutes(0);
    sunday.setSeconds(0);
    sunday.setMilliseconds(0);
    if (sunday < now) sunday.setDate(sunday.getDate() + 7); // Make sure it's future
    millisecondsLeft = sunday - now;
    

    如果是sunday &lt; now,您可以在每次通过时检查,如果是,则重新计算sunday

    (如果有人想知道,如果您在周日下午计算sunday,则应该触发“确保它是未来”这一行......)

    【讨论】:

    • 从逻辑上讲,您的代码非常合理,但我似乎无法让它工作,我一定是做错了什么。
    【解决方案2】:

    尝试添加这个:

    var timerhelp = 'yes';
    jQuery(document).ready(function($) {
    
         nextSunday();
    
    if (timerhelp == 'yes') {
        var init = setInterval(animation, 900); // did change to 900, no need to calculate it more than once a second :)
    }
    function animation(){       
        var deadline1 = $('.time').attr('rel');
        var deadline2 = $('.time').attr('contents');
        var now = new Date();
        now = Math.floor(now / 1000);
        now = now + Math.floor(deadline2 * 60 * 60);
        var counter1 = deadline1 - now;
        var seconds1=Math.floor(counter1 % 60);
        if (seconds1 < 10 && seconds1 > 0 ){
            seconds1 = '0'+seconds1;
    
        }
        counter1=counter1/60;
        var minutes1=Math.floor(counter1 % 60);
        if (minutes1 < 10 && minutes1 > 0){
            minutes1 = '0'+minutes1;
    
        }
        counter1=counter1/60;
        var hours1=Math.floor(counter1 % 24);
        if (hours1 < 10 && hours1 > 0){
            hours1 = '0'+hours1;
        }
        counter1=counter1/24;
        var days1=Math.floor(counter1);
        if (days1 < 10 && days1 > 0){
            days1 = '0'+days1;
        }
      if(days1<0&&hours1<0&&minutes1<0&&seconds1<0){
     nextSunday();
    }
        $('.time').html('<table><tbody><tr><th class="day">'+days1+'</th><th class="day">'+hours1+'</th><th class="day">'+minutes1+'</th><th class="day">'+seconds1+'</th></tr><tr><th>Days</th><th>Hours</th><th>Min</th><th>Sec</th></tr></tbody></table>');
    
    
    }
    });
    
    function nextSunday(){ 
    var now = new Date().getTime() / 1000; // time in seconds;
    var rel = $('.time').attr('rel');
        if(parseInt(now)>parseInt(rel)){
            var n = parseInt(rel)
            while(n<parseInt(now)){
                n =  n+604800;
            }
            $('.time').attr('rel',n);
        }
    }
    

    【讨论】:

    • 不幸的是,这似乎没有做,仍然得到负值。
    • 问题似乎出在var counter1 = deadline1 - now; 使用我的答案来更改deadline1 值。
    • 请使用Amadan的解决方案,很漂亮。
    • 试过了,还是负值。
    • 请看这个:jsfiddle.net/2PxsA/2你仍然需要做出改变。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-10
    • 2011-08-01
    • 2021-05-25
    • 2014-05-10
    • 1970-01-01
    • 1970-01-01
    • 2021-07-26
    相关资源
    最近更新 更多