title: Javascript Event Synchronization
body: <p>I'm building a concert calendar that's very heavy on javascript (using jQuery). I'm having trouble synchronizing various events throughout the app, and I'm looking for suggestions for how to do this.</p>
  
  <p>An example of a simple use case: </p>
  
  <ol>
  <li>User clicks a month</li>
  <li>Calendar skips to that month</li>
  </ol>
  
  <p>An example of a more complex use case:</p>
  
  <ol>
  <li>User selects an artist</li>
  <li>Calendar determines the date of that artist's first show</li>
  <li>Calendar skips to that month</li>
  <li>Calendar highlights that artist's show(s)</li>
  </ol>
  
  <p>One occasional problem is that the new month isn't yet rendered by the time I try to highlight the artist's show(s). Thus, the show isn't highlighted even though these functions are called in order. Obviously, using setTimeout() is pretty hacky, and not guaranteed to work.</p>
  
  <p>So first, a simple question -- would it ever be possible (even in Chrome) for the following function to run out of sequence?</p>
  
  <pre><code>function steps(){
  
      stepOne(); //TAKES 30 SECONDS
  
      stepTwo(); //TAKES &lt; 1 SECOND
  
  }
  </code></pre>
  
  <p>Second, a related simple question:</p>
  
  <pre>
      If placed at the end of a function, will a JS callback ALWAYS run after 
      everything else in the given function has finished running?
  </pre>
  
  <p>If so, I <em>could</em> nest each function as a callback of its previous function. But that would likely become unwieldy, once you consider all the different use cases.</p>
  
  <p>Here's an approach I'm considering:</p>
  
  <pre>
      1) Allow each function an optional callback parameter. If it's present,
         call it at the very end of the function.
  
      2) During a UI refresh, create an array, and stack any functions to be
         called within this array.
  
      3) Once this "script" is completed, iterate through the array, calling
         each function in the order it was added, using the next function 
         as the previous function's callback.
  </pre>
  
  <p>I imagine this would ensure that all the functions are called in order.</p>
  
  <p>Another approach is to attach event listeners using</p>
  
  <pre><code>$(document).bind("listener.name", fnCallback);
  </code></pre>
  
  <p>And then calling</p>
  
  <pre><code>$(document).trigger("listener.name");
  </code></pre>
  
  <p>Whenever that event occurs.</p>
  
  <p>However, I'm guessing this would be kind of unwieldy as well, considering different events might need to call different sets of functions depending on the use case. I could always call</p>
  
  <pre><code>$(document).unbind("listener.name");
  </code></pre>
  
  <p>before adding new events to it, but again -- I'm leaning toward creating sort of a master "script" as I suggested in the first approach.</p>
  
  <p>Hopefully this isn't too vague -- any feedback? Any experience with complex UIs that had to synchronize various events?</p>
  
  <p>Thanks very much,</p>
  
  <p>Michael</p>