Hello. Interested in whether it is possible to perform actions on jQuery or JS at the appointed time. That is, I have a page that I need to be forced to refresh / reload at one o'clock. Or is it possible to do this using PHP / Python / server side?
1 answer
There is no scheduler that can be accessed via javascript in the browser. Refresh the page directly through php is also impossible.
Solution to the head: every 50 seconds check the current hour and minute and reload the page at the right moment. Additionally, you need to save the date of the last reload so that the page does not reload all the minute. An interval of 50 seconds is chosen to ensure that the test is triggered at least once per minute (setIneterval has an error).
var lastReloadDate; setInterval(function () { var now = new Date(); if (now.getHours() == 13 && now.getMinutes() == 0 && now.getDate() !== lastReloadDate) { lastReloadDate = now.getDate(); location.reload(); } }, 50000); In addition, you can keep a permanent connection to the server via long polling or WebSockets, determine the right moment on the server and send a reload command to the browser.
Of course, for this page must be open in the browser. To force the browser to open any page from the server is impossible. If you want the page to be updated or opened at the right time, the only solution I see is to write a small browser extension. But for his work, a natural, running browser is needed.
- 2Why check every n seconds? Why it is impossible to calculate the difference between the premenu when it will be necessary to reload the page and the current time, and not to crush the result in setTimeout? - Arnial 4:08 pm
- @Arnial, yes, it will be more reasonable, did not think about such an optimization. - Alexey Ukolov
- 2It is better not to use
setIntervalbecause of the peculiarities of the work. It is better to check the condition at the end and set a newsetTimeout. - user207618 4:41 pm - @Other, what are the features of the work? - Alexey Ukolov
- 2@ AlekseyUkolov, habrahabr.ru/post/138062 - user207618
JavaScript,JQueryorPHP?cronnot suitable? Here I made a very detailed instruction. (Yes, it is the same). - Sasha Chernykh