title: JavaScript in a web browser: Can I make it event driven instead of looping?
body: <p>I am working on a robotics project where I control a robot using a joystick and the keys on my keyboard via a browser and a web server on the robot.</p>
  <p>Right now the basic (browser side) implementation is a series of functional blocks that read the position of the joystick, the state of the joystick's buttons, and if certain keys have been pressed, sending this information to the server on the robot, using &quot;requestAnimationFrame&quot; as the looping mechanism.</p>
  <p>The problem I am experiencing is that the connection between the browser and the robot is too &quot;chatty&quot; - there being updates almost continuously, even if nothing is happening.</p>
  <p>Because of this, if I want to watch for a specific behavior on the server side, I cannot because the one useful message is swamped in (literally) thousands of &quot;nothing happened!&quot; messages.</p>
  <p>The big problem here is the joystick itself as, as far as I know, there are two and only two events - connected and disconnected. Once the joystick status is &quot;connected&quot; the browser sends zillions of messages.</p>
  <p>I tried using the timer object in the joystick API data structure as a way of creating a &quot;something happened&quot; event</p>
  <p>Viz.:</p>
  <pre><code>function is_something_happening(jsdata, gopigo3_joystick) {
    var old_time = gopigo3_joystick.time_stamp
    while (old_time == Number.parseFloat(jsdata.timestamp).toFixed()) {
      ;  // null statement so that this just spins. . . .
      return;
    }
  }
  </code></pre>
  <p>What that does is cause the browser to freeze until the next joystick event happens.</p>
  <p>Is there some way to program this so that <em><strong>nothing happens</strong></em> until I do something?</p>
  <p>Something like a &quot;you go do whatever you want, and when I need you I'll call you&quot; kind of an interrupt or event driven thing - especially for the joystick?</p>
  <p>Or, perhaps, there's a better way of debugging and/or trying to track specific events than the brute-force methods I'm using?</p>
  <p>=================</p>
  <p>Update:</p>
  <p>Thanks for <em><strong>ALL</strong></em> the great ideas!</p>
  <p>Let me clarify the problem now that I've gotten some feedback:</p>
  <p>I am using an event listener for the keypresses and individual keypresses are sent to the robot to be acted upon, (or ignored), as the case may be.  (<em>i.e.</em> Is this a key I'm interested in?)  And the keyboard event routines work wonderfully - they only fire when <em><strong>I actually press a key</strong></em>.  If the joystick isn't running, nothing happens until I press a key.</p>
  <p>For the joystick, (<em>i.e.</em> &quot;gamepad&quot;), there are only two native events, one that fires if the gamepad is connected and one that fires when it is disconnected.  Period, and that's it.</p>
  <p>All, (and I do mean <em><strong>ALL</strong></em>), of the gamepad API demo's and examples assume that you're writing a game that will be played within the browser context so they all use <code>requestAnimationFrame()</code> as the driving element of their &quot;game loop&quot; and things happen at v_Sync speeds or whatever the browser allows.</p>
  <p>Because of this, the gamepad object doesn't generate events after it's been started, it just runs and runs.</p>
  <p>The only distinguishing factor is the gamepad's time_stamp attribute which increments whenever the gamepad is actually active, hence my use of <code>gopigo3_joystick.time_stamp</code> in my wait loop.</p>
  <p>Obviously, I need to create a custom event that, somehow or other, can encapsulate the wait for the timer to increment, and only then allow the gamepad to send messages.</p>
  <p>However, as far as I can tell, creating a custom event would simply move the spin-lock, (wink!), from the wait loop to the custom event as I read that customEvent()'s are synchronous - and I translate that to mean &quot;blocking&quot;.</p>
  <p>Is it possible to create an event that depends on a change in <code>gopigo3_joystick.time_stamp</code>, that will go off by itself and not block the rest of the browser's activity?</p>
  <p>Allow me to apologize in advance - this is all virgin territory for me and I do appreciate all the advice and guidance you can give.</p>