maztt
maztt

Reputation: 12294

setinterval vs settimeout

How can run a countdown timer at 7:50 AM for 10 minutes until 8:00AM everyday. After that the shift will close. the timer will be a warning for finishing the work.

I used different flavours of setinterval and settimeout code snippets for some hours now. but i don't know how to proceed.My main question is to use setinterval or setimeout.

1) setinterval: is checking the that the time is 7:50 after every few minutes is ok?

2) settimeout: is it ok to count the seconds of the day. and then proceed with calling the function after those seconds?

This works for me

window.setInterval(function() {
    var date = new Date();
    if (date.getHours() === 8 && date.getMinutes() === 0) {}
}, 60000);



var now = new Date();
var millisTill10 = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 10, 0, 0, 0) - now;
if (millisTill10 < 0) {
    millisTill10 += 86400000;
}
setTimeout(function() {
    alert("It's 10am!")
}, millisTill10);

Upvotes: 0

Views: 1494

Answers (2)

Taha Paksu
Taha Paksu

Reputation: 15616

setInterval is used to repeat a callback function with a given time, setTimeout is used to run a callback after a specific amount of time. Since you need to create a counter, you can use setInterval here.

Note: If you want to display the users every sconds in 10 minutes, you may use 1000 as the interval timing value. But if you want to show every minutes in the 10 minute duration, then using 60 * 1000 as the interval timing value is better.

setInterval(function(){
   var dateNow = new Date();
   if(dateNow.getHours() >= 7 && 
      dateNow.getMinutes >= 50 && 
      dateNow.getHours < 8)
   {
       // if the alert box isn't displayed yet
       // display it first.
       // update the display.
   }else{
       // if the alert box is displayed
       // hide it
   }
}, 1000); // or 1000 * 60 for minute based display

Upvotes: 1

Charlie
Charlie

Reputation: 23778

Run a timer for every minute. Show your warning if the current time falls within your allocation. The problem with this code is that it is only accurate up to a minute - not up to the second.

And running this every second is not a good practice.

setInterval(function(){

   var d = new Date();
   var h = d.getHours();
   var m = d.getMinutes();

   //Rough estimation for the time between 7.50 and 8.00 here
   if (h === 7 && m >= 50) 
      console.log('Warning!');     


}, 1000)

Now we can do more...

We can get the above routine to kick-start a timeout function which is going to be precise in the interval. It will trigger a countdown timer at the correct time and set alarm for another 24 hours.

var starter = setInterval(function(){

   var d = new Date();
   var h = d.getHours();
   var m = d.getMinutes();

   //Rough estimation for the time between 7.50 and 8.00 here
   if (h === 7 && m >= 50) 
      setTimeout(timer24(), 1000 * 60 * 24)  


}, 1000)


function timer24(){

   //Warn every 10 seconds
   var countdown = setInterval(function(){

       console.log('Warning!'); 

       var d = new Date();
       var h = d.getHours();

       if (h == 8)
          clearInterval(countdown)   

    }, 10)

    setTimeout(timer, 1000 * 60 * 24)

}

Upvotes: 1

Related Questions