简体   繁体   English

如何防止倒计时计数器在同一秒内生成超过 1 个值?

[英]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.如果等于或小于100毫秒,我输入else并向另一个function发送请求。 There is a problem here, the counter generates more than one value under 1 millisecond and enters the else repeatedly.这里有一个问题,计数器在1毫秒内生成多个值,并重复进入else。 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.或者,如果我在 1 秒内将条件写入 else function 作为条件,它会产生类似 969,871,771 的值并重复输入 else function。 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.看看这是否有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JavaScript计数器-如何在同一页面上包含多个计数器? - javascript counter - how to include more than one counter on the same page? 如何将计数器值显示到输出中以开始倒计时 - how to display counter value into the output to start a countdown Moment.js倒数不会减少一秒以上 - Moment.js countdown not decrementing more than a second 如果超过 60 分钟,如何使计数器从小时开始? - How to make counter start from hour if more than 60 minutes? 正则表达式防止同一字符连续出现超过 3 次 - Regex to prevent the same character from appearing more than 3 times in a row 如何防止用户使用jquery在同一个单词中重复三次以上的字符 - How to prevent the user from repeating a character more than three times in the same word using jquery 如何集成动态倒数计数器,该计数器从WordPress中的自定义字段获取日期值? - How can I integrate a dynamic countdown counter which gets the date value from a custom field in WordPress? 如何防止桌子上随机生成的图像在同一台上生成<td> ? - How to prevent randomly generated images on table from generating on same <td>? 防止同一个请求不止一次 - prevent the same request done more than once 如何防止模态多次被绘制? - How to prevent a modal from being drawn more than once?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM