每周三的JS脚本倒计时?

时间:2018-03-15 05:15:56

标签: javascript

我已经看过很多脚本的例子,这些脚本可以倒计时到特定的日期/时间......然后停在那里。

如果倒计时到下周三(并且在达到当前星期三时重置到下周三)?更具体地说,我期待在星期三,日本标准时间上午10:30倒计时。

这是我到目前为止所拥有的:

jQuery(document).ready(function($) {
    var wednesday = nextWednesday();
    wednesday = Math.floor(wednesday / 1000);

    var init = setInterval(function() {
        var now = new Date();
        now = Math.floor(now / 1000);

        var counter = wednesday - now;
        var seconds = Math.floor(counter % 60);

        counter = counter/60;
        var minutes = Math.floor(counter % 60);

        counter = counter/60;
        var hours = Math.floor(counter % 24);

        counter = counter/24;
        var days = Math.floor(counter);

        if (days < 0 && hours < 0 && minutes < 0 && seconds < 0) {
            nextWednesday();
        }

        $('.time').html(days+' days, '+hours+' hours, '+minutes+' minutes, '+seconds+' seconds');
    }, 900);
});

function nextWednesday() { 
    var now = new Date();
    var wed = new Date();
    wed.setDate(now.getDate() - now.getDay()); // <-- how to make this next Wednesday?
    wed.setHours(10);
    wed.setMinutes(30);
    wed.setSeconds(0);
    wed.setMilliseconds(0);
    if (wed < now) wed.setDate(wed.getDate() + 7); // <-- Does this accurately set to next Wednesday?

    return wed;
}
<html>
    <head>
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

    </head>
    
    <body>
      <div class="time"></div>
    </body>
</html>

我觉得我很接近,不太确定如何让它永远是下周三日本标准时间上午10:30。

谢谢!

0 个答案:

没有答案