0
I want to put a timer on a page, the function works, shows the time progressively, but I realized that the use of "setInterval()" begins to consume a lot of page processing until the point of the browser lock for security.
Why then I want to calculate the difference of the current time and the time that opened the page. There is another way to do?
function tempo(){
var hora = document.getElementById('hora');
var data = new Date();
hora.value = data.toLocaleTimeString('pt-BR');
verificarHorario();
setInterval(tempo ,1000);
}
function verificarDiferencaHorario(inicialMin, finalMin) {
var totalMin = Number(finalMin - inicialMin);
var hora2 = document.getElementById('hora2');
hora2.value = ((Math.trunc(totalMin / 60).toString() + ":" + (totalMin % 60).toString()));
}
function verificarHorario() {
var inicial = "11:20"; //teste
var hora = document.getElementById('hora');
var final = "12:20"; //teste
var splInicial = inicial.split(":"), splFinal = final.split(":");
var inicialMin = (Number(splInicial[0] * 60)) + Number(splInicial[1]);
var finalMin = (Number(splFinal[0] * 60)) + Number(splFinal[1]);
verificarDiferencaHorario(inicialMin, finalMin);
}
It did not work, the input does not receive and the variable date does not update. What solved my problem was to remove the setInterval from the function time and put outside the scope of the function. thus: var myVar = setInterval(time, 1000);
– GabrielLocalhost