停止倒数计时器的故障排除



我创建了一个简单的倒数计时器来显示 45 秒的倒计时。我正在尝试使用 JavaScript onclick 函数通过两个按钮来控制这个计时器。一个按钮将启动计时器,一个按钮将停止它。我能够启动时间并让它显示在屏幕上,但无法停止它。我在网上找到了时间脚本,并对其进行了修改以仅显示秒数。

我试图创建一个全局 id(在本例中为"var xx;"(来清除间隔,但没有奏效。我不确定缺少什么。以下是我使用 Bootstrap 4 的代码的重要部分。

// create a global variable to reset the interval later
var xx;
// Visible Coundown 45s function
function VisibleCountDownTimer45s() {
// reset internval if it is already in defined.
if(xx != undefined) {
clearInterval(xx)
};
// Set the date we're counting down to
var countDownDate = new Date().getTime() + 45000;
// Update the count down every 1 second
var xx = setInterval(function() {
// Get today's date and time
var now = new Date().getTime();
// Find the distance between now and the count down date
var distance = countDownDate - now;
// console.log (distance);
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
// Display the result in the element with id="demo"
document.getElementById("demo").innerHTML =  seconds + "s ";
// If the count down is finished, write some text
if (distance < 0) {
clearInterval(xx);
document.getElementById("demo").innerHTML = "";
}
}, 1000);
}	
<!-- timer display box -->
<div class="container">
<div class="row mb-3">
<div class="col-md-4">&nbsp;</div>
<div class="col-md-4 text-center pb-3" id="demo"><span class="border border-secondary p-2">show     timer here</span></div>
<div class="col-md4">&nbsp;</div>	
</div>
</div>
<!-- control buttons -->
<div class="container">
<div class="row">
<div class="col-md-4 text-right"><button class="btn btn-primary" onclick="VisibleCountDownTimer45s()">Start visible Timer</button></div>
<div class="col-md-4 text-center" id="Counter"><p>&nbsp;</p></div>
<div class="col-md4 text-left"><button class="btn btn-primary" onclick="clearInterval(xx)">Stop visible Timer</button></div>	
</div>
</div>

您的问题是在VisibleCountDownTimer45s()函数内部,您正在重新声明一个名为 xx 的本地范围的变量。删除那里的var关键字,您将把计时器分配给其他函数可以访问的全局xx变量。这称为范围问题。

更改:var xx = setInterval(function() {

收件人:xx = setInterval(function() {

看到这个:

// create a global variable to reset the interval later
var xx;
// Visible Coundown 45s function
function VisibleCountDownTimer45s() {
// reset internval if it is already in defined.
if (xx != undefined) {
clearInterval(xx)
};
// Set the date we're counting down to
var countDownDate = new Date().getTime() + 45000;
// Update the count down every 1 second
xx = setInterval(function() {
// Get today's date and time
var now = new Date().getTime();
// Find the distance between now and the count down date
var distance = countDownDate - now;
// console.log (distance);
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
// Display the result in the element with id="demo"
document.getElementById("demo").innerHTML = seconds + "s ";
// If the count down is finished, write some text
if (distance < 0) {
clearInterval(xx);
document.getElementById("demo").innerHTML = "";
}
}, 1000);
}
<div class="container">
<div class="row mb-3">
<div class="col-md-4">&nbsp;</div>
<div class="col-md-4 text-center pb-3" id="demo"><span class="border border-secondary p-2">show     timer here</span></div>
<div class="col-md4">&nbsp;</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-md-4 text-right"><button class="btn btn-primary" onclick="VisibleCountDownTimer45s()">Start visible Timer</button></div>
<div class="col-md-4 text-center" id="Counter">
<p>&nbsp;</p>
</div>
<div class="col-md4 text-left"><button class="btn btn-primary" onclick="clearInterval(xx)">Stop visible Timer</button></div>
</div>
</div>

还值得注意的是,无需使用 Date 对象并计算相对于它的经过时间,也可以获得相同的结果。

let timer;
function startTimer(interval) {
if (timer !== undefined) {
clearInterval(timer);
};
timer = setInterval(function() {
interval -= 1000
if (interval <= 0) {
clearInterval(timer);
document.getElementById("demo").innerHTML = "";
} else {
const seconds = Math.floor(interval / 1000);
document.getElementById("demo").innerHTML = seconds + "s ";
}
}, 1000);
}
<div class="container">
<div class="row mb-3">
<div class="col-md-4">&nbsp;</div>
<div class="col-md-4 text-center pb-3" id="demo"><span class="border border-secondary p-2">show     timer here</span></div>
<div class="col-md4">&nbsp;</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-md-4 text-right"><button class="btn btn-primary" onclick="startTimer(45000)">Start visible Timer</button></div>
<div class="col-md-4 text-center" id="Counter">
<p>&nbsp;</p>
</div>
<div class="col-md4 text-left"><button class="btn btn-primary" onclick="clearInterval(timer)">Stop visible Timer</button></div>
</div>
</div>

最新更新