2013-07-26 96 views
1

我在我的網站上倒計時,我希望它在倒數到下一個星期天上午11:00,當它的星期日11:01 A.M我希望它自動倒計時到下午的星期天上午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>'); 


} 
}); 

回答

0

嘗試添加此:

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); 
    } 
} 
+0

那似乎不幸的是沒有做,仍然得到負值。 – user2608501

+0

這個問題似乎在'var counter1 = deadline1 - now;'用我的答案來改變deadline1的值。 –

+0

請使用Amadan的解決方案,它很漂亮。 –

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 < now,並重新計算sunday如果是。

(如果你想知道,在「確保它的未來:」如果你在週日下午計算sunday線應觸發...)

+0

從邏輯上說,你的代碼很有意義,但我似乎無法讓它起作用,我一定在做錯事。 – user2608501

相關問題