I have a function that runs every frame on my website. Inside it, there is a key press check, that if I press a key, it gets fired at least a 100 times. How can I prevent this?
I've tried slowing down my FPS using a wait function, but to get it to work I have to slow it down to around 5 FPS, which is too slow.
Here is the code inside the function that runs forever (I'm using socket.io, so there is a bit of that in there. Don't mind it):
    // Rendering
    $('#svg').empty();
    for (var i = 0; i < players.length; i++){
      var circle = makeSVG('circle', {cx: players[i].x, cy: players[i].y, r: '5vw', stroke: '#00FFFF', 'stroke-width': '0px', fill: '#00FFFF'})
      document.getElementById('svg').appendChild(circle);
    }
    // Key Press
    $(document).keydown(function(e) {
      switch(e.which) {
          case 37: // left
            socket.emit('update', 'left');
            break;
          case 38: // up
            socket.emit('update', 'up');
            break;
          case 39: // right
            socket.emit('update', 'right');
            break;
          case 40: // down
            socket.emit('update', 'down');
            break;
          default:
          break;
      }
      e.preventDefault(); // prevent the default action (scroll / move caret)
    });
    socket.emit('update', 'No Keys')
I want the key press even to be sent 5 (about) times a second, not 60-100 like I am experiencing.
 
    