Question:
How can I trigger a JavaScript function at a predefined time of day, say 10:00 AM?
Solution:
To schedule a function execution at a specific time, you can leverage JavaScript's setTimeout() method in conjunction with Date to calculate the remaining time.
<code class="javascript">var now = new Date(); var millisTill10 = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 10, 0, 0, 0) - now; if (millisTill10 < 0) { millisTill10 += 86400000; // adjust for tomorrow if past 10 AM } setTimeout(function() { // Your function to be executed at 10 AM }, millisTill10);</code>
Explanation:
The above is the detailed content of How to Execute a JavaScript Function at a Specific Time of Day?. For more information, please follow other related articles on the PHP Chinese website!