Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
115
rated 0 times [  116] [ 1]  / answers: 1 / hits: 16914  / 15 Years ago, thu, april 23, 2009, 12:00:00

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.


More From » datetime

 Answers
10

You have to compute the number of milliseconds between now and your date object:


function setToHappen(fn, date){
return setTimeout(fn, date - Date.now());
}

NB Please note @calvin's answer: this will not work if the number of milliseconds is greater than 2147483647.


[#99662] Friday, April 17, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jarrodfletchers

Total Points: 75
Total Questions: 94
Total Answers: 95

Location: Netherlands
Member since Thu, Jul 1, 2021
3 Years ago
;