JSFiddle - React, Tailwind, and code Playground

by LyndseyB

HTML

<div id="js-countdown"></div>

JavaScript

(function() {
	var timer = 10,
  		startTime = getCurrentTime(),
			endTime = startTime + timer,
    	countdown = document.getElementById('js-countdown'),
      counter;    	   
      
  function getCurrentTime() {
  	return Math.floor(Date.now() / 1000);
  }
  
  function updateTimer() {    
    var curTime = getCurrentTime(),
        diff = curTime - startTime;

    countdown.innerHTML = (timer - diff);

    if(curTime === endTime) {
      clearInterval(counter);
      endGame();
    }
  }   
      
  function startGame() {      
  	updateTimer();
    counter = setInterval(updateTimer, 100);
  }
  
  function endGame() {
  	console.log("Game ended!");
  }
  
 	startGame();
  
})();