Search code examples
javascriptcountdown

precise js daily repeat countdown timer


I've got this timer, which works just fine if I want it to count down to a straight hour, like 09:00. But it has to count down to 9:15 every day, and then reset. Now this is what my code currently looks like:

<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>

Now I know there is a more effecient way of doing this, I just cant seem to figure it out. Any help is much appreciated.


Solution

  • You can find a much easy to implement hope this help. You can set what ever time you want. Just a notice this works only for a day and is in 24 hours system. This works even if the month changes.

    Please let me know if you have any questions.

    <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);