精确的js每日重复倒数计时器

时间:2015-08-24 11:05:37

标签: javascript countdown

我有这个计时器,如果我想让它倒数到一个连续的小时,就像09:00一样。但它必须每天倒数到9:15,然后重置。 现在这就是我的代码目前的样子:

<script>
var date;
var display = document.getElementById('time'); 
setInterval(function()
{ 
    date = new Date();

    var currentHours = date.getHours();
            var currentMinutes = date.getMinutes();

    var hours;
            var minutes;
            var seconds;

    if(currentHours == 09 && currentMinutes < 15)
    {
                minutes = 14 - date.getMinutes();
                seconds = 60 - date.getSeconds();
                display.innerHTML = 'Tid til næste bestilling: ' + minutes + ' minutter, ' + seconds + ' sekunder.';
    }
    else if(currentHours == 09 && currentMinutes > 15)
    {
                hours = 08 + (24 - currentHours);
                minutes = 74 - date.getMinutes();
                seconds = 60 - date.getSeconds();
                display.innerHTML = 'Tid til næste bestilling: ' + hours + ' timer, ' + minutes + ' minutter, ' + seconds + ' sekunder.';
    }
    else if(currentHours < 09)
    {
                hours = 08 - currentHours;
                minutes = 60 - date.getMinutes();
                seconds = 60 - date.getSeconds();
                display.innerHTML = 'Tid til næste bestilling: ' + hours + ' timer, ' + minutes + ' minutter, ' + seconds + ' sekunder.';
    }
    else if(currentHours > 09)
    {
                hours = 08 + (24 - currentHours);
                minutes = 60 - date.getMinutes();
                seconds = 60 - date.getSeconds();
                display.innerHTML = 'Tid til næste bestilling: ' + hours + ' timer, ' + minutes + ' minutter, ' + seconds + ' sekunder.';
    }
            else if(currentHours == 09 && currentMinutes == 15)
            {
                display.innerHTML = 'Bestillinger afsendt. Timeren starter om lidt igen.';
            }
}
,1000);
</script>

现在我知道有一种更有效的方法可以做到这一点,我似乎无法弄明白。 非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

你可以找到一个很容易实现的希望这个帮助。你可以设置你想要的时间。只是一个通知,这只适用于一天,并在24小时制度。即使月份发生变化,这也有效。

如果您有任何疑问,请与我们联系。

<script>
var date;
var display = document.getElementById('time'); 
setInterval(function()
{ 
    date = new Date();
    date1 = new Date(); 
    // you can set the hours, minutes, seconds here this is the date co compare
    date1.setHours(9);
    date1.setMinutes(15);
    date1.setSeconds(0);

    // here you make a date diff
    var diff =0;
    if(date1 < date){
        console.log('true');
        date1.setDate(date1.getDate()+1);
        diff = date1-date;
    }else{
    console.log('false');
        diff = date1-date;
    }

    // to avoid divided by 0
    if(diff ==0){       
        display.innerHTML = date+ ''+ date1 + ''+ date2+ ' Tid til næste bestilling: 24 timer, 0 minutter, 0 sekunder.';
        return;
    }
    // calculate hours, minutes, seconds
    hours = Math.floor(diff/(1000*60*60));
    diff -= hours *1000*60*60
    minutes = Math.floor(diff/(1000*60));
    diff -= minutes *1000*60;
    seconds = Math.floor(diff/(1000));  

    //finaly display
    display.innerHTML = d' Tid til næste bestilling: ' + hours + ' timer, ' + minutes + ' minutter, ' + seconds + ' sekunder.';

}
,1000);