title: how to exit out of a document.onkeydown function in javascript?
body: <p>I would like to, when x reaches 5, exit out of a document.onkeydown function. I tried this:</p>
  
  <pre><code>var x = 0
  
  document.onkeydown = function() {
    console.log("Still running...")
  
    x += 1
    if (x &gt;= 5) {
      break
    }
  }
  </code></pre>
  
  <p>but the console says that it was an illegal break statement. I have also tried using a <code>return</code> statement to exit out of the onkeydown loop, but that hasn't worked either. Do any of you know how to exit out of one of these? If so, I would very much like to know it.</p>