简体   繁体   English

Javascript - 如何在用户获胜时停止游戏中的计时器

[英]Javascript - how to stop a timer from a game when the user wins

I've been taking an online course for web development.我一直在参加 web 开发的在线课程。 After doing a project to create a game I decided to modify it to get some practice.在完成了一个创建游戏的项目后,我决定对其进行修改以进行一些练习。 One of the things I'm trying to add is a timer for a player's turn, the code for the timer I got from a question posted online.我要添加的一件事是轮到玩家的计时器,这是我从在线发布的问题中获得的计时器代码。 What I haven't figured out is how to stop the function once the user clicks on the correct answer:我还没有想到的是,一旦用户点击正确答案,如何停止 function:

function timer(sec){
    var timeleft = sec;
    var downloadTimer = setInterval(function(){
  if(timeleft <= 0){
    clearInterval(downloadTimer);
    document.getElementById("countdown").textContent = "Time's Up!";
  } else {
    document.getElementById("countdown").textContent = timeleft;
  }
  timeleft -= 1;
    }, 1000);
}

This function runs once the player start's his/her turn.这个 function 在玩家开始时运行。 I tried putting that code inside a While loop so that the code runs as long as the clicked answer is not equal to the correct answer but it looks like it just starts looping and the page stops responding.我尝试将该代码放在 While 循环中,以便只要单击的答案不等于正确答案,代码就会运行,但看起来它只是开始循环并且页面停止响应。

I also tried setting the timeleft variable to 0 once the player gets the right answer but that doesn't work either.一旦玩家得到正确答案,我也尝试将 timeleft 变量设置为 0,但这也不起作用。

Any help or ideas would be really appreciated.任何帮助或想法将不胜感激。

try:尝试:

function timer(sec){ //Create a function with parameter named "sec"
    var timeleft = sec; //Make the value of the variable "timeleft" to value of parameter "sec"

    var downloadTimer = setInterval(function(){ //Create a loop and name it "downloadTimer"

  if (timeleft == 0){ //Check if the value of "timeleft" == 0

    clearInterval(downloadTimer); //The same with "break"

    document.getElementById("countdown").innerHTML = "Time's Up!"; "Displays the text: "Time's Up!"

  }

  timeleft -= 1; //The same with timeleft = timeleft - 1

document.getElementById("countdown").innerHTML = timeleft; //Display the time left

    }, 1000); //Loop every 1 second (1000ms = 1 second)
}

or try to set boolean:或尝试设置 boolean:

var b = false; //Make a new variable
var timeleft = 0; //This too

setInterval(function() {
if (b == true) { //Check if the variable b = true

timeleft -= 1; //The same with timeleft = timeleft - 1

document.getElementById("countdown").innerHTML = "Timeleft: " + timeleft; 
//Uhh... This will show the time left
}
else { //if b == false

timeleft = 0 //Set timer value to default
}
if (timeleft == 0) { //Check if the value of variable "timeleft" == 0 

    b = false //Set boolean to false so you can stop the timer

    document.getElementById("countdown").innerHTML = "Time's Up!"; //Change the text of element with id = "countdown"
  }
    }, 1000); //This will loop the function every 1 second  (1000ms = 1 second)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM