title: How to pause simple canvas game, made with js and html5?
body: <p>I created a simple snake game after following some simple tutorials on YouTube. </p>
  
  <p>The problem is that the game does not have a pause function (e.g. when pressing P the game should pause/resume) and when the snake hits the border of the canvas the game restarts itself (but that is another problem). </p>
  
  <p>Here is the complete code I have of the game: <a href="https://pastebin.com/URaDxSvF" rel="nofollow noreferrer">https://pastebin.com/URaDxSvF</a></p>
  
  <p>The pause-related functions I've created:</p>
  
  <pre><code>function gamePaused{ /**i need help on this**/ }
  
  function keyDown(e) {
    if (e.keyCode == 80) pauseGame();
  }
  
  function pauseGame() {
    if (!gamePaused) {
      game = clearTimeout(game);
      gamePaused = true;
    } else if (gamePaused) {
      game = setTimeout(loop, 1000 / 30);
      gamePaused = false;
    }
  }
  </code></pre>