Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
CodeHS JavaScript定时器-倒计时_Javascript_Countdowntimer - Fatal编程技术网

CodeHS JavaScript定时器-倒计时

CodeHS JavaScript定时器-倒计时,javascript,countdowntimer,Javascript,Countdowntimer,我在编写代码,需要为我正在制作的游戏“突破”中的加电设置一个倒计时。我需要定时器是可重用的,所以没有循环,它需要在秒/毫秒(不管是哪一个)下降,最好持续30秒或30000毫秒。请记住,这是我正在处理的代码。请告诉我我是否错了,因为我不知道代码是什么,但我非常确定,这可以通过一个简单的setInterval函数实现 以整秒计算: var timer=30; setInterval(function(){ timer-=1; document.getElementByI

我在编写代码,需要为我正在制作的游戏“突破”中的加电设置一个倒计时。我需要定时器是可重用的,所以没有循环,它需要在秒/毫秒(不管是哪一个)下降,最好持续30秒或30000毫秒。请记住,这是我正在处理的代码。

请告诉我我是否错了,因为我不知道代码是什么,但我非常确定,这可以通过一个简单的
setInterval
函数实现

以整秒计算:

   var timer=30;
    setInterval(function(){
    timer-=1;
    document.getElementById(timerId). innerHTML=timer;//shows the remaining time
    }, 1000);//subtracts 1 second from timer each second
以十分之一秒的速度前进

var timer=30.0;
setInterval(function(){
timer-=0.1;
document.getElementById(timerId). innerHTML=timer;//shows the remaining time
}, 1000);//subtracts a tenth second from timer every 0.1 seconds

如果您想在启动计时器30秒后发生某些事情,可以执行以下操作:

//set the time remaining to 30 outside of a function so it is global
var timeRemaining = 30;

function start(){
//set the timer to run the function countdown once every second(1000 milliseconds)
    setTimer(countdown, 1000)
}

function countdown(){
    /*every time it is called this function subtracts one from the time if it is
    more than 30, when it reaches 0 stops the timer and resets the time*/
    if(timeRemaining<=0){
        stopTimer(countdown);
        timeRemaining = 30;
        println("Done");
        //30 seconds has passed, do what you need here(call your function)
    }else{
        timeRemaining--;
        println(timeRemaining+" seconds left")
    }
}
//将函数外部剩余时间设置为30,使其为全局时间
var剩余时间=30;
函数start(){
//将计时器设置为每秒运行一次功能倒计时(1000毫秒)
设置计时器(倒计时,1000)
}
函数倒计时(){
/*每次调用该函数时,如果是,则从时间中减去一
超过30,当达到0时,停止计时器并重置时间*/

if(timeremaining)我确信在正常情况下这会起作用,但是CodeHS并没有定义你要输入的很多命令。CodeHS对于像我这样的初学者来说是一个有点麻木的版本。请展示你所做的(你的代码)。
//set the time remaining to 30 outside of a function so it is global
var timeRemaining = 30;

function start(){
//set the timer to run the function countdown once every second(1000 milliseconds)
    setTimer(countdown, 1000)
}

function countdown(){
    /*every time it is called this function subtracts one from the time if it is
    more than 30, when it reaches 0 stops the timer and resets the time*/
    if(timeRemaining<=0){
        stopTimer(countdown);
        timeRemaining = 30;
        println("Done");
        //30 seconds has passed, do what you need here(call your function)
    }else{
        timeRemaining--;
        println(timeRemaining+" seconds left")
    }
}