Added a countdown timer
This commit is contained in:
parent
70a782d0e4
commit
89a6b4d062
6 changed files with 115 additions and 16 deletions
35
site/mental-arithmatic/scripts/countdown.js
Normal file
35
site/mental-arithmatic/scripts/countdown.js
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
import { Controller } from "https://unpkg.com/@hotwired/stimulus/dist/stimulus.js";
|
||||
|
||||
export class CountdownController extends Controller {
|
||||
static targets = ["countdown", "fakeinput"];
|
||||
|
||||
start() {
|
||||
this.element.classList.remove('hidden');
|
||||
|
||||
this._countdown = 3;
|
||||
this.countdownTarget.innerText = this._countdown;
|
||||
|
||||
this._tickInterval = window.setInterval(this._tick.bind(this), 1000);
|
||||
window.setTimeout(() => {
|
||||
this.fakeinputTarget.focus();
|
||||
}, 1);
|
||||
}
|
||||
|
||||
_tick() {
|
||||
this._countdown -= 1;
|
||||
|
||||
if (this._countdown === 0) {
|
||||
this.countdownTarget.innerText = "GO!";
|
||||
} else if (this._countdown < 0) {
|
||||
window.clearInterval(this._tickInterval);
|
||||
this._startActualGame();
|
||||
} else {
|
||||
this.countdownTarget.innerText = this._countdown;
|
||||
}
|
||||
}
|
||||
|
||||
_startActualGame() {
|
||||
this.element.classList.add('hidden');
|
||||
window.dispatchEvent(new CustomEvent("startGame"));
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue