tommy popping game timer

game timer framework with pause, resume, status, extended play. Any second iteration can potentially spawn any action.

by Mark Hendricks

HTML

<button id="playPause">
  Play Game
</button><br>
Score: <span id="score"></span><br>
HighScore: <span id="highScore"></span><br>
Status: <span id="gameStatus"></span><br>
Timer: <span id="gameTimer">00:00</span><br>

JavaScript

var consoleLog = true;
var paused = false;
var initReq = true;
var highScore = 0;
var score = generateRandomIntegerInRange(0, 30); // pseudo score
const gameButton = document.getElementById("playPause");
const gameStatus = document.getElementById("gameStatus");
const timeDisplay = document.getElementById("gameTimer");
const hscore = document.getElementById("highScore");

function init() {
  second = 1000; // 1 sec = 1000 ms
  maxTime = 20; // Base Time
  extTime = 10; // Extended Time
  s = 0; //second count
  xScore = 20; // score must be gt or equal for extended play
  extPlay = false; 

}

function waitforme(millisec) {
	return new Promise(resolve => {
		setTimeout(() => { resolve('') }, millisec);
	})
}
  
async function gameTime() {
	// Play Game
	for (s = 0; s < maxTime + 1; ++s) {
  	if (paused) return;
		await waitforme(second);
    countDown = maxTime - s; /* reverse count */
    status = displayTime(countDown);
    updateConsole(status);

		// Warning: Time is almost up!
    if (countDown == 5) {
      status = updateGameStatus("Time Is Almost Up!");
    	updateConsole(status);
    }
        
 		// Extended Play
    if (s == maxTime-1){    
			if (score >= xScore && extPlay == false) {
				maxTime = maxTime+extTime;
				extPlay = true;
  			status = updateGameStatus("Extended Play");
    		updateConsole(status);
			}
		}
	}
	gameOver();
}

function displayTime(seconds) {
	gameTimer.innerHTML = pad(seconds);
  return seconds;
}

function gameOver() {
  status = updateGameStatus("Game Over");
	updateConsole(status);
  updateGameButton("Play Again");
  
  // loadData(); // may have changed since initial pageload
  
  if (score > highScore) highScore = score;
  updateHighScore(highScore);
  
  // updateGameCounter();
  // storeData(); 
  
  // reset for new game
	initReq = true;
	init();
  score = generateRandomIntegerInRange(0, 30); // remove
}

// Generate a random number within range including the min and max
function generateRandomIntegerInRange(min,...