0

After executing My code , the function instantly executes and shows the alerted message. my intent is to correct it to show the alert at the exact specified time.

Here is my code :

var millisTill10 = new Date(new Date().getFullYear(),new Date().getMonth(),
 new Date().getDay(), 1, 16, 59, 999) - Date.now();

setTimeout(function(){alert("It's the time!")}, millisTill10);
nhab
  • 1
  • 2
  • 2
    On page load you can calculate the time offset until the time you want to display the alert, and then use `setTimeout()` to trigger it in x milliseconds. https://www.tutorialspoint.com/How-to-delay-a-JavaScript-function-call-using-JavaScript or https://stackoverflow.com/questions/17883692/how-to-set-time-delay-in-javascript/26306356 or https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout for more info – WOUNDEDStevenJones Apr 24 '20 at 20:57
  • Does this answer your question? [In JavaScript, how can I have a function run at a specific time?](https://stackoverflow.com/questions/24741530/in-javascript-how-can-i-have-a-function-run-at-a-specific-time) – Juan Pablo Moreno Martín Apr 24 '20 at 20:59
  • @JuanPabloMorenoMartín helped. how can I give "point" to your comment? – nhab Apr 24 '20 at 21:47
  • I did not comment, I raised a duplicate flag. – Juan Pablo Moreno Martín Apr 24 '20 at 22:30

0 Answers0