2017-06-12 132 views
0

我正在嘗試創建一個每週倒計時計時器。jQuery每週倒計時計時器

我有一個彈出窗口設置爲只出現在星期五,星期六和星期日。

我需要一個jQuery定時器,它從星期五00:00開始,到週日24:00結束,並且每週都會工作。

我在這裏找到這個的jsfiddle - https://jsfiddle.net/cwqxzm6o/151/

function plural(s, i) { 
    return i + ' ' + (i > 1 ? s + 's' : s); 
} 

function sundayDelta(offset) { 
    // offset is in hours, so convert to miliseconds 
    offset = offset ? offset * 60 * 60 * 1000 : 0; 
    var now = new Date(new Date().getTime() + offset); 
    var days = 7 - now.getDay() || 7; 
    var hours = 24 - now.getHours(); 
    var minutes = now.getMinutes() - 00 ; 
    var seconds = now.getSeconds()- 00; 
    if (hours < 0){ 
     days=days-1; 
    } 
    var positiveHours= -hours>0 ? 24-(-hours) : hours; 
    return [plural('day', days), 
      plural('hour', positiveHours), 
      plural('minute', minutes), 
      plural('second', seconds)].join(' '); 
} 

// Save reference to the DIV 
$refresh = $('#refresh'); 

$refresh.text('This page will refresh in ' + sundayDelta()); 

// Update DIV contents every second 
setInterval(function() { 
    $refresh.text('This page will refresh in ' + sundayDelta()); 
}, 1000); 

然而,當我在我的天更改爲週日倒計時進入「7天24小時等」的時候,應該說「0天24小時等「

有人可以租我把我的痛苦,給我一個更新小提琴的作品?

+0

你要刷新頁面時「」 7天24小時等」意志來 –

回答

0

如果要在日等於7且小時等於24時刷新頁面,則必須使用location.reload()進行刷新。 只是改變我們的代碼

 function plural(s, i) { 
     return i + ' ' + (i > 1 ? s + 's' : s); 
    } 
    function reloadPage(){ 
     location.reload(); 
    } 

    function sundayDelta(offset) { 
     // offset is in hours, so convert to miliseconds 
     offset = offset ? offset * 60 * 60 * 1000 : 0; 
     var now = new Date(new Date().getTime() + offset); 
     var days = 7 - now.getDay() || 7; 
     var hours = 24 - now.getHours(); 
     var minutes = 60 - now.getMinutes(); 
     var seconds = 60 - now.getSeconds(); 
     if(now.getDay() == 7 && now.getHours() == 24){ 
      reloadPage(); 
     } 
     return [plural('day', days), 
      plural('hour', hours), 
      plural('minute', minutes), 
      plural('second', seconds)].join(' '); 
    } 

    // Save reference to the DIV 
    $refresh = $('#refresh'); 

    $refresh.text('This page will refresh in ' + sundayDelta()); 

    // Update DIV contents every second 
    setInterval(function() { 
     $refresh.text('This page will refresh in ' + sundayDelta()); 
    }, 1000); 

您可以點擊這裏https://jsfiddle.net/Bibhudatta_sahoo/cwqxzm6o/153/ 這將刷新頁面,你和重置你的倒計時。

+0

是的,但?如果你改變你的日期到星期天,它會顯示「7天等」它應該讀取,因爲它需要在星期日倒數到24:00。 – Jamie

0

我很確定這是由於now.getDay()在星期天返回零的事實,因此如果days等於零,您需要堅持進行測試。目前你的數學是做7 - 0所以返回7.

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Date/getDay

Updated fiddle

+0

謝謝你解釋和更新小提琴。一個星期天,它目前說12小時28分(目前這裏是12點33分),不應該讀11小時28分嗎? – Jamie

+0

https://jsfiddle.net/089vkxr2/ 您只需將小時數改爲23,將分鐘數改爲59即可獲得偏移量。 – PhilipAbbott

+0

嗨@Jamie如果這個或任何答案已經解決了您的問題,請考慮通過點擊複選標記來接受它。這向更廣泛的社區表明,您已經找到了解決方案,併爲答覆者和您自己提供了一些聲譽。沒有義務這樣做。 – PhilipAbbott