-2

Just wondering, if there is any way or codes, that can set countdown timer for every 6hours, for everyday?

For example, i want to start countdown at this timing(In other words, like every 6 hours): 9am-3pm 3pm-9pm 9pm-3am 3am-9am?

I have look all over the site, and couldn't find a countdown timer like that..

Preferably in HTML and 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 に答える