Answer the question
In order to leave comments, you need to log in
Why is the timer not reset in js?
I’m doing a countdown, I ran into magic, when the page is updated, for some reason the timer doesn’t reset
, I start to google for everyone, the problem is exactly one turn - what’s the matter? how to make the timer start again after page refresh?
var now = new Date(),
times = [
0 - now.getHours(),
25 - now.getMinutes(),
59 - now.getSeconds(),
],
mBox = document.getElementById('minutes'),
sBox = document.getElementById('seconds');
clearInterval(times)
timer_live(times);
function timer_live(times) {
var tm = setInterval(function () {
var hour = times[0],
min = times[1],
sec = times[2];
times[2]--;
if (times[0] == 0 && times[1] == 0 && times[2] == 0) {
clearInterval(tm);
} else if (times[2] == -1) {
times[1]--;
times[2] = 59;
} else if (times[1] == -1) {
times[0]--;
times[1] = 59;
}
var hour = (times[0] < 10) ? '0' + times[0] : times[0],
min = (times[1] < 10) ? '0' + times[1] : times[1],
sec = (times[2] < 10) ? '0' + times[2] : times[2];
showTimer(hour, min, sec);
}, 1000);
};
function showTimer(hour, min, sec) {
mBox.innerHTML = min;
sBox.innerHTML = sec;
}
Answer the question
In order to leave comments, you need to log in
const arr = [];
const object = { name: 'Example' };
arr.findIndex(element => element.name === 'Example'); // можно через arr.find , но он вернёт найденный объект
This will clearInterval(times)
not reset the timer for you, because you need to specify not an array of times as an argument , but the result of tm returned by the function tm = setInterval(...);
.
Probably because the initial value of the timer is tied to the current time
var now = new Date(),
times = [
0 - now.getHours(),
25 - now.getMinutes(),
59 - now.getSeconds(),
],
clearInterval(times)
doesn't work the way you probably expect it to. It is needed to cancel the start of the interval timer. It takes the id of this timer as input, but you pass an array with the value of the timer there.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question