-2

只是想知道,如果有任何方法或代码,可以为每天每 6 小时设置倒数计时器吗?

例如,我想在这个时间开始倒计时(换句话说,就像每 6 小时一样):上午 9 点到下午 3 点 下午 3 点到晚上 9 点 下午 9 点到凌晨 3 点 上午 3 点到上午 9 点?

我翻遍了整个网站,找不到这样的倒数计时器..

最好在 HTML 和 javascript/jquery 中。

4

2 回答 2

2
var timer = {
        started: false,
        timestamp: 0
    },
    trigger = 15;                          //3pm

function timerInit(){
    var hour = new Date().getHours();
    if(!started && hour === trigger){      //When it's 3pm, the timer will start
        startTimer();                      //└─ rewrite the conditional statement 
                                           //   as needed
        //do whatever you want here
        timer.timestamp = +new Date();
        timer.started = true;              //Indicates the timer has been started
    }
    requestAnimationFrame(timerInit);      //setTimeout is not efficient
}
requestAnimationFrame(timerInit);


//This is for when the timer has ended.
function timerEnded(){
    timer.started = false;
}

function startTimer(){
    var d = new Date();
    timePassed = new Date(timer.timestamp + 1000*60*60*6 - d);
    var remaining = {                      //Calculate time difference
        hour: timePassed.getHours(),       // using timestamps
        minute: timePassed.getMinutes(),
        second: timePassed.getSeconds()
    }
    console.log(remaining);
    if(timePassed > 0){
        setTimeout(startTimer, 500);       //Countdown
    }else{
        timerEnded();                      //Ended
    }
}

http://jsfiddle.net/DerekL/kKPcr/7/

于 2013-01-29T04:25:50.537 回答
-2

您可以使用javascript的本机setInterval方法

例如:

var timer = setInterval(myfunction, 21600) // 21600 seconds == 6 hours
于 2013-01-29T04:19:14.490 回答