简体   繁体   中英

How to prevent the countdown counter from generating more than 1 value in the same second?

In the code below, I'm generating the remaining value as seconds. If it is equal to or less than 100 milliseconds, I enter else and send a request to another function. There is a problem here, the counter generates more than one value under 1 millisecond and enters the else repeatedly. Or if I write the condition inside the else function under 1 second as a condition, it produces values like 969,871,771 and enters else function repeatedly. How can I fix this?

function reset() {
        }
        function millisToMinutesAndSeconds(millis) {
            var seconds = ((millis % 60000) / 1000).toFixed(0);
            return (seconds < 10 ? "0" : "") + seconds;
        }
        setInterval(function () {
            var remaining = 25000 - (Date.now() % 25000);
            if (remaining > 100) {
                document.getElementById("timer").innerText =
                    millisToMinutesAndSeconds(remaining);
            } else {
                tensecond();
            }
        }, 100);


function tensecond() {
            }
            function millisToMinutesAndSeconds(millis) {
                var seconds = ((millis % 60000) / 1000).toFixed(0);
                return (seconds < 10 ? "0" : "") + seconds;
            }
            setInterval(function () {
                var remaining = 10000 - (Date.now() % 10000);
                if (remaining > 100) {
                    document.getElementById("timer").innerText =
                        millisToMinutesAndSeconds(remaining);
                } else {
                    reset();
                }
            }, 100);
function counter(value, resetValue) {
  if (value === 0) counter(resetValue, resetValue === 10 ? 25 : 10);
  const use_this_value = value.toString().padStart(2, '0');
  setTimeout(() => counter(value--, resetValue), 1000);
}

see if this is helpful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM