Consider the need to invoke a JavaScript function at a precise time each day, such as 10:00 AM. This task can be achieved through a combination of JavaScript functionality and the manipulation of time-related values.
The provided code attempts to open a new browser window at 10:00 AM and then repeatedly every minute. However, it needs refinement to function correctly.
To modify the code for accurate scheduling:
<script type="text/javascript"><br>var now = new Date();<br>var millisTill10 = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 10, 0, 0, 0) - now;</p> <p>if (millisTill10 < 0) {</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">millisTill10 += 86400000; // it's after 10am, try 10am tomorrow.
}
setTimeout(function() {
openAPage(); setInterval(openAPage, 60 * 1000); // Set the interval for subsequent calls
}, millisTill10);
function openAPage() {
// Your function to perform the desired actions
}
The code has been modified as follows:
By incorporating these changes, the provided code will now correctly execute the desired function at 10:00 AM and continue to do so every minute.
The above is the detailed content of How to Schedule a JavaScript Function to Run at a Specific Time of Day?. For more information, please follow other related articles on the PHP Chinese website!