gpt4 book ai didi

javascript - 暂停时倒数计时器javascript错误

转载 作者:行者123 更新时间:2023-11-29 10:37:42 28 4
gpt4 key购买 nike

我正在用 javascript 编写一个简单的倒数计时器,带有播放和暂停按钮。当我启动它时它工作正常,但如果我暂停/播放几次计时器值随机变化,我无法弄清楚为什么。

这是我的代码:

var minutesleft = 60;
var secondsleft = 0;
var millisecondsleft = 0;
var bool = true;
var paused = false;
var end;
var now;

function pause(){
paused = true;
}

function stop(){
end = now;
bool = true;
}

function cd(){
if (bool) {
end = new Date();
end.setMinutes(end.getMinutes()+minutesleft);
end.setSeconds(end.getSeconds()+secondsleft);
end.setMilliseconds(end.getMilliseconds()+millisecondsleft);
bool = false;
}
now = new Date();
diff = end - now;
diff = new Date(diff);
var msec = diff.getMilliseconds();
var sec = diff.getSeconds();
var min = diff.getMinutes();
if (min < 10){
min = "0" + min;
}
if (sec < 10){
sec = "0" + sec;
}
if(msec < 10){
msec = "00" +msec;
}
else if(msec < 100){
msec = "0" +msec;
}
if(now >= end){
clearTimeout(timerID);
document.getElementById("cdtime").innerHTML = 'POLICE IS HERE';
}
else{
document.getElementById("cdtime").innerHTML = min + ":" + sec + ":" + msec;
}
if (paused == false){
timerID = setTimeout("cd()", 10);
}
else {
bool = true;
minutesleft = min;
secondsleft = sec;
millisecondsleft = msec;
}
paused = false;
}

这是一个 fiddle :https://jsfiddle.net/cw3s5124/

提前致谢

最佳答案

跳跃数字的主要问题是由于字符串连接然后在日期函数中使用该字符串。部分代码如下:

if (min < 10){
min = "0" + min;
}

一个例子证明为什么会发生这种情况

var   end1 = new Date(); 
var minutesleft1 = 9;
console.log("Minutes to assign to current time");
console.log(end1.getMinutes() + minutesleft1); // Logs expected amount of minutes to calculate
end1.setMinutes(end1.getMinutes() + minutesleft1);

var end2 = new Date();
var minutesleft2 = "09";
console.log("Wrong Minutes to assign to current time");
console.log(end2.getMinutes() + minutesleft2); // Logs big number
end2.setMinutes(end2.getMinutes() + minutesleft2);

console.log(end1); // Logs expected result
console.log(end2); // Logs a time a day or so in the future

说明:如果您要在分钟变量中将开始计时器设置为 9,则需要立即用 0 填充以显示。由于您现在将 minutesleft 变量设置为 "09",因此您在调用 end 时实际上是在执行 9 + "09"。 setMinutes(end.getMinutes()+minutesleft);

将字符串 "0" 连接到数字后,实际上是在尝试对字符串进行数字运算,这会导致意外结果。这在开始时没有发生,因为您的 if 语句尚未运行,因此 minsec msec 变量仍然是数字。 您应该只在显示数字时填充数字,而不是计算本身。

我用从 this post 借用的填充函数替换了字符串连接.

我还添加了一个播放功能来重置您的暂停参数。这使代码更具可读性,也更容易让我理解。此外,将其更改为在开始时暂停,因为计时器尚未运行。

var minutesleft = 10;
var secondsleft = 05; // This would normally just be returned as 5, that's why we need the pad function for display
var millisecondsleft = 0;
var firstCall = true;
var paused = true;
var end;
var now;

function pause() {
paused = true;
}

function play() {
if (paused === false) // Shorcut out because we are already running
return;

paused = false;
cd();
}

function stop() {
end = now;
paused = true;
cd();
}

document.addEventListener("DOMContentLoaded", function(event) {
document.getElementById("cdtime").innerHTML = pad(minutesleft, 2) + ":" + pad(secondsleft, 2) + ":" + pad(millisecondsleft, 2);
});

function cd() {
if (firstCall) {
end = new Date();
end.setMinutes(end.getMinutes() + minutesleft);
end.setSeconds(end.getSeconds() + secondsleft);
end.setMilliseconds(end.getMilliseconds() + millisecondsleft);
firstCall = false;
}
now = new Date();
diff = end - now;
diff = new Date(diff);
var msec = diff.getMilliseconds();
var sec = diff.getSeconds();
var min = diff.getMinutes();

if (now >= end) {
clearTimeout(timerID);
document.getElementById("cdtime").innerHTML = 'POLICE IS HERE';
} else {
document.getElementById("cdtime").innerHTML = pad(min, 2) + ":" + pad(sec, 2) + ":" + pad(msec, 2);
}
if (paused === false) {
timerID = setTimeout("cd()", 10);
} else {
bool = true;
minutesleft = min;
secondsleft = sec;
millisecondsleft = msec;
}
}

function pad(n, width, z) {
z = z || '0';
n = n + '';
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
}
<body>
<div id='timer'>
<button class='playerButtons' id='playT' type='submit' onclick='play()'>Play</button>
<button class='playerButtons' id='pauseT' type=' submit' onclick='pause()'>Pause</button>
<button class='playerButtons' id='stopT' type='submit' onclick='stop()'>Stop</button>
<div id='cdtime'></div>
</div>
</body>

关于javascript - 暂停时倒数计时器javascript错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34183472/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com