Premium Features

Download Video

Game Timers and Game Over

Now we need some sort of method to end our game when time runs out, or when either our player or enemy's health bar is fully depleted. We'll code this functionality using JavaScript's setTimeout function, and monitor our fighters' health using something custom.

Comments

Want to participate?

Create a free Chris Courses account to begin

Login
p
peepeepoopoooo posted 4 years ago

at 4:30, my clock does NOT do countdown what does it mean?

this is my index.js:

let timer = 10 
function decreaseTimer(){
   // infinite loop = requestAnimationFrame(animate)
  if (timer > 0) {
    setTimeout(decreaseTimer, 1000)
    timer--
    document.querySelector('#timer').innerHTML = timer // innerHTML = cái số 10 á 
  }

  if (player.health === enemy.health){
    console.log('tie')
  }
}

and this is my index.html:

<!-- timer -->
      <div id="timer"
      style="
        background-color: red; 
        width: 100px;
        height: 100px;
        flex-shrink: 0;
        display: flex;  
        align-items: center; 
        justify-content: center;">
      10
      </div>
0
p
peepeepoopoooo posted 4 years ago

oh merry me! i forgot to call the function "decreaseTimer()" afterwards for it to work! thank you anyways

0
XxCFxX posted 4 years ago

I keep getting this error for cancel timeout! index.js:134 Uncaught ReferenceError: cancelTimeout is not defined

at determineWinner (index.js:134:3)

at animate (index.js:212:5)

0
chris posted 4 years ago

Should be clearTimeout, I made the same mistake in the vid 😅

0
XxCFxX posted 4 years ago

ty! :)

0

Providing the lift to launch your development career

© 2026 Chris Courses. All rights reserved.