F
F
ff0xff2020-02-10 13:26:26
JavaScript
ff0xff, 2020-02-10 13:26:26

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

5 answer(s)
V
Vladimir, 2019-05-30
@Casufi

https://jsfiddle.net/cfdzvsmt/1/

K
kova1ev, 2019-05-30
@kova1ev

If the object has some unique field, like id, look for it.

E
Egor Zhivagin, 2019-05-30
@Krasnodar_etc

const arr = [];
const object = { name: 'Example' };

arr.findIndex(element => element.name === 'Example'); // можно через arr.find , но он вернёт найденный объект

A
AUser0, 2020-02-10
@ff0xff

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(...);.

A
Athanor, 2020-02-10
@Athanor

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(),
                                ],

UPDATE
also, the line 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 question

Ask a Question

731 491 924 answers to any question