[英]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.