Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make JavaScript interval synchronize with actual time

I want to make the seconds synchronize with the real time (new Date()) seconds, so that it would work if the page was loaded mid second.

setInterval(() => {
    console.log("hey im not synchronized");
}, 1000);

Is that possible?

like image 545
Pecius Avatar asked Oct 28 '25 09:10

Pecius


1 Answers

Is that possible?

Yep, like this:

setTimeout(() => {
    setInterval(() => console.log("hey im synchronized"), 1000)
}, 1000 - new Date().getMilliseconds());
like image 166
Nurbol Alpysbayev Avatar answered Oct 31 '25 07:10

Nurbol Alpysbayev



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!