1
Gentlemen, I have a doubt, there is some method that for example performs at a certain time?
I would like when it was 0h the page would update automatically, then save some cookie to inform that that day has already been updated..
1
Gentlemen, I have a doubt, there is some method that for example performs at a certain time?
I would like when it was 0h the page would update automatically, then save some cookie to inform that that day has already been updated..
2
This function here works perfectly for this, just switch to the desired times and use the function reload()
to refresh the page.
var now = new Date();
var millisTill10 = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 10, 0, 0, 0) - now;
if (millisTill10 < 0) {
millisTill10 += 86400000; // já passou das 10 da manhã, começa de novo.
}
setTimeout(function(){alert("São 10 da manhã!")}, millisTill10);
2
I don’t think it is possible to build and persist a cookie with only pure HTML and Javascript.
The most root solution to doing something every day at midnight, just with Javascript, would be something like:
var dezSegundos = 10000; // dez segundos em milissegundos
var quinzeSegundos = 15000;
var verificaHora() {
var agora = new Date();
var hoje = new Date(agora.getFullYear(), agora.getMonth(), agora.GetDate());
var msDesdeMeiaNoite = agora.getTime() - hoje.getTime();
if (msDesdeMeiaNoite < quinzeSegundos) {
// Aqui você executa sua lógica
}
}
setInterval(verificaHora, dezSegundos);
Explanation: the code runs every five minutes - use setInterval instead of setTimeout for the code to continue running for several consecutive days. So if it’s the first time it runs in a day, it does something that you’ve implemented.
To complete, as the setInterval has no guarantee of executing exactly at the instant for which it was programmed, we give a five-second break for him to run.
What if I gave a Reload on the problem page in the flow of the ranges? And it ran once always midnight or the first time of the day?
If you reload the page, you lose the threads that were running. If you’re going to reload then whatever setTimeout
as to the setInterval
. The problem with this is that if the recharge takes too long you may end up losing time. It is worth trying all the alternatives suggested here and see which one best solves the problem.
0
the language does not give this type of freedom even because it is very rare to use, what you can do is take the hour:minute:second at the time the page is loaded, calculate how many ms are left for the next day and put what you want within a setTimeOut.
-1
I did so
setInterval(function () {
var agora = new Date();
var a = `${agora}`;
var resultado = a.split(" ");
console.log(resultado[4]);
if (resultado[4] == '00:00:00') {
console.log('aqui fica sua função');
}
}, 1000);
Browser other questions tagged javascript html
You are not signed in. Login or sign up in order to post.
@Leocaracciolo no, will work only once. To work more than once, use setInterval instead of setTimeout.
– Oralista de Sistemas
@Renan think neither of the two because the time has to be 0hs and the way I put will run after 24 hours of access to page. Agree?
– user60252
@I don’t know, because you removed the code. But the difference between the methods is that one executes the reported function only once, while the other repeats the execution at each pass of the interval.
– Oralista de Sistemas