How does JavaScript realize loop detection of keystrokes?

problem description

I want to do loop detection on keys several times, such as using for loop to detect buttons three times, and each time I detect that the button is triggered, I will execute the specified function, such as I press the space (keyCode=32), then print 1 in the background, and press three times to print out three 1s.
but the problem I have now is that three 1s will pop up once, with the code as follows:

<html>
  <script type="text/javascript">
    for(let i=0;i<3;iPP){
        document.addEventListener("keydown",function(){
            var e = event || window.event || arguments.callee.caller.arguments[0];
            if(e.keyCode!=null&&e.keyCode==32){
                console.log("ok");
            }
        })
    }
  </script>
</html>

ask the boss to answer questions ~


just remove the for statement;


addEventListener executes three times to add three keystroke listening events to document, so there are three 1


for loops


arguments.callee.caller.arguments [0] seems to report an error on Firefox. Change the following to
about counting

(function(){
 var idx = 0; 
 document.addEventListener("keydown",function(e){
  var e = e || window.event ||event;
  if(e.keyCode&&e.keyCode==32){
    idxPP;
    console.log(idx);
  }
 });
})();
Menu