Javascript clearTimeout不工作:参数未定义(即使它在全局范围内定义)
我的setTimeout()函数可以工作,但我的clearTimeout()不能工作。即使我有一个“if”语句,该语句应该在变量“secs”小于0时运行clearTimeout函数,但计时器仍会一直向下倒计时到负数。当我在控制台中输入变量名“secs”时,我得到了未定义的变量名,即使它被定义为setTimeout调用的函数中的一个参数。我不知道我做错了什么。有人能帮忙吗 我的完整代码在 下面是JavaScript代码片段:Javascript clearTimeout不工作:参数未定义(即使它在全局范围内定义),javascript,cleartimeout,Javascript,Cleartimeout,我的setTimeout()函数可以工作,但我的clearTimeout()不能工作。即使我有一个“if”语句,该语句应该在变量“secs”小于0时运行clearTimeout函数,但计时器仍会一直向下倒计时到负数。当我在控制台中输入变量名“secs”时,我得到了未定义的变量名,即使它被定义为setTimeout调用的函数中的一个参数。我不知道我做错了什么。有人能帮忙吗 我的完整代码在 下面是JavaScript代码片段: function startTimer(secs, elem) {
function startTimer(secs, elem) {
t = $(elem);
t.innerHTML = "00:" + secs;
if(secs<0) {
clearTimeout(countDown);
}
secs--;
//recurring function
countDown = setTimeout('startTimer('+secs+',"'+elem+'")', 1000);
}
功能启动计时器(秒、元素){
t=$(elem);
t、 innerHTML=“00:”+秒;
if(secs添加一个条件来调用递归函数,如下所示
if (secs < 0) {
secs = secsInput;
}
//recurring function
countDown = setTimeout('startTimer('+secs+',"'+elem+'")', 1000);
if(秒<0){
secs=secsInput;
}
//循环函数
倒计时=设置超时('startTimer('+secs+',“'+elem+”),1000);
对于倒计时计时器,我建议使用setInterval
和clearInterval
代替。setInterval
将为您重复运行回调函数。它可能如下所示:
倒计时;
功能启动计时器(秒、元素){
倒计时=设置间隔(函数(){
t=$(elem);
t、 innerHTML=“00:”+秒;
秒--
如果(秒<0){
清除间隔(倒计时);
}
}, 1000);
}
在您的情况下,使用setInterval
和clearInterval
更方便
要保留setTimeout
和clearTimeout
功能,应在if语句中添加return
function startTimer(secs, elem) {
t = $(elem);
t.innerHTML = "00:" + secs;
if(secs<0) {
clearTimeout(countDown);
return;
}
secs--;
//recurring function
countDown = setTimeout('startTimer('+secs+',"'+elem+'")', 1000);
}
功能启动计时器(秒、元素){
t=$(elem);
t、 innerHTML=“00:”+秒;
如果在调用clearTimeout(countDown)
时(秒,countDown
指的是上一个已经超时的超时。它不会停止尚未启动的超时。您不能重新设置超时,如
if(!/*finished*/) setTimeout(startTimer, 1000, secs, elem);
因此,我认为计时器必须处理4个事件:
测验开始了
测验结束了
计时器用完了
运动员回答一个问题
这可以通过函数返回带有一些选项的对象来解决。
createTimer可用于设置计时器的参数
点1。将是计时器。start()-->将使用参数启动计时器
第3点。可以通过在计时器用完时调用的回调来解决-->createTimer(5,'display',()=>{//您的代码在这里})
第2点。可通过-->timer.stop()实现
当需要在不耗尽计时器的情况下重置计时器时,需要第4点。重置()
此外,间隔不在全局范围内,因此您可以有多个具有不同设置的计时器,并且它们不会相互干扰
// function for creating the timer
function createTimer(seconds, cssSelector, callbackOnTimeout) {
// interval the timer is running
let interval;
// the html node where innerText will be set
const display = document.getElementById(cssSelector)
// original seconds passt to createTimer needed for restart
const initSec = seconds
// starting or continuing the interval
function start() {
// setting interval to the active interval
interval = setInterval(() => {
display.innerText = `00:${seconds}`;
--seconds;
if (seconds < 0) {
// calling restart and callback to restart
callbackOnTimeout()
restart()
}
}, 1000);
}
// just stopping but not resetting so calling start will continue the timer
// player takes a break
function stop(){
clearInterval(interval)
}
// opted for a restart and not only a reset since it seemed more appropriate for your problem
function restart(){
clearInterval(interval)
seconds = initSec
start()
}
// returning the object with the functions
return {
start: start,
stop: stop,
restart: restart
}
}
// example for creating a timer
const timer1 = createTimer(5,'display',()=>{
console.log(`you where to slow ohhh...`)
})
// calling the timer
timer1.start()
//用于创建计时器的函数
函数createTimer(秒、cssSelector、callbackOnTimeout){
//计时器正在运行的时间间隔
让间隔;
//将在其中设置innerText的html节点
const display=document.getElementById(cssSelector)
//重新启动所需createTimer的原始秒数
常数initSec=秒
//开始或继续间隔
函数start(){
//将间隔设置为活动间隔
间隔=设置间隔(()=>{
display.innerText=`00:${seconds}`;
--秒;
如果(秒<0){
//调用restart和回调以重新启动
callbackOnTimeout()
重新启动()
}
}, 1000);
}
//只是停止但不重置,因此调用start将继续计时
//球员休息一下
函数停止(){
清除间隔(间隔)
}
//选择重新启动,而不仅仅是重置,因为它似乎更适合您的问题
函数重新启动(){
清除间隔(间隔)
秒=初始秒
开始()
}
//返回带有函数的对象
返回{
开始:开始,
停:停,,
重启:重启
}
}
//创建计时器的示例
常量timer1=createTimer(5,'display',()=>{
log(`you where to slow ohhh…`)
})
//调用计时器
timer1.start()
谢谢Allabakash。这是可行的,但计时器完全停止在0。我忘了提到计时器需要一直返回到5并倒计时——而不仅仅是在它达到0后停止。好的,你的意思是说它永远不会停止?继续运行,当它达到0时,它应该从5开始?@RB50然后你需要重置secs
到5
,而不是试图停止超时的代码是的,我每次都需要它在5点重新启动。我有这样的代码,但它仍然不起作用。@RB50好的,更新了答案,签出并让我知道它是否有帮助。根本不需要调用clearTimeout
,当该代码恢复时没有活动超时uns。