Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When seconds come x, do something

How I will do that: When seconds come 31 (example 11:57 31sec.) do somthing, every minute. Using Javasript.

Thanks in advance.

like image 297
ilhan Avatar asked Aug 10 '26 06:08

ilhan


2 Answers

Read the current time, calculate the number of seconds until the next time the seconds is ':31' then use setTimeout with the appropriate delay. You could use something like this:

var atSeconds = 31;
var secondsLeft = atSeconds - new Date().getSeconds();
if (secondsLeft <= 0) secondsLeft += 60;
setTimeout(foo, secondsLeft * 1000);

Remember to call it again in the function foo so that it repeats.

like image 66
Mark Byers Avatar answered Aug 11 '26 19:08

Mark Byers


Something like this will probably be as close as you can get.

function initializeInterval() {
    while (new Date().getSeconds() < 30);
    setInterval(doWork, 60000);
}

function doWork() {

}
like image 26
ChaosPandion Avatar answered Aug 11 '26 21:08

ChaosPandion



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!