Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setTimeout but for a given time

Is there anything readily available in JavaScript (i.e. not through "plugins") that allows me to do something like setTimeout, but instead of saying in how many milliseconds something should happen, I give it a date object telling it when to do something?

setToHappen(function () {
    alert('Wake up!');
}, new Date("..."));

And yes, I know I can do this by simply subtracting new Date() with my existing date object (or maybe it's the other way around) to get the amount of milliseconds, but I'd still like to know.

like image 493
Deniz Dogan Avatar asked Apr 23 '09 09:04

Deniz Dogan


People also ask

What is alternative to setTimeout?

The setInterval method has the same syntax as setTimeout : let timerId = setInterval(func|code, [delay], [arg1], [arg2], ...) All arguments have the same meaning. But unlike setTimeout it runs the function not only once, but regularly after the given interval of time.

Is setTimeout in seconds or milliseconds?

Definition and Usage. The setTimeout() method calls a function after a number of milliseconds. 1 second = 1000 milliseconds.

Is setTimeout a callback function?

Introduction to JavaScript setTimeout()cb is a callback function to be executed after the timer expires. delay is the time in milliseconds that the timer should wait before executing the callback function. If you omit it, the delay defaults to 0.


1 Answers

Since people are talking about calculating timeout intervals using date objects, it should be noted that the max value setTimeout() will accept for the interval parameter is 2147483647 (2^31 - 1) as PRIntervalTime is a signed 32-bit integer. That comes out to just under 25 days.

like image 197
Calvin Avatar answered Oct 28 '22 15:10

Calvin