How do I prevent the concurrent execution of a javascript function?
<p>I am making a ticker similar to the "From the AP" one at The Huffington Post, using jQuery. The ticker rotates through a ul, either by user command (clicking an arrow) or by an auto-scroll.</p>

<p>Each list-item is display:none by default. It is revealed by the addition of a "showHeadline" class which is display:list-item. HTML for the UL Looks like this:</p>

<pre><code>        &lt;ul class="news" id="news"&gt;
            &lt;li class="tickerTitle showHeadline"&gt;Test Entry&lt;/li&gt;
            &lt;li class="tickerTitle"&gt;Test Entry2&lt;/li&gt;
            &lt;li class="tickerTitle"&gt;Test Entry3&lt;/li&gt;
        &lt;/ul&gt;
</code></pre>

<p>When the user clicks the right arrow, or the auto-scroll setTimeout goes off, it runs a tickForward() function:</p>

<pre><code>function tickForward(){
  var $active = $('#news li.showHeadline');
  var $next = $active.next();
  if($next.length==0) $next = $('#news li:first');
  $active.stop(true, true);
  $active.fadeOut('slow', function() {$active.removeClass('showHeadline');});

  setTimeout(function(){$next.fadeIn('slow', function(){$next.addClass('showHeadline');})}, 1000);
  if(isPaused == true){
  }
  else{
          startScroll()
  }
     };
</code></pre>

<p>This is heavily inspired by <a href="http://jonraasch.com/blog/a-simple-jquery-slideshow" rel="nofollow">Jon Raasch's A Simple jQuery Slideshow.</a>
Basically, find what's visible, what should be visible next, make the visible thing fade and remove the class that marks it as visible, then fade in the next thing and add the class that makes it visible.</p>

<p>Now, everything is hunky-dory if the auto-scroll is running, kicking off tickForward() once every three seconds. But if the user clicks the arrow button repeatedly, it creates two negative conditions:</p>

<ol>
<li>Rather than advance quickly through the list for just the number of clicks made, it continues scrolling at a faster-than-normal rate indefinitely.</li>
<li>It can produce a situation where two (or more) list items are given the .showHeadline class, so there's overlap on the list.</li>
</ol>

<p>I can see these happening (especially #2) because the tickForward() function can run concurrently with itself, producing different sets of $active and $next.</p>

<p>So I think my question is: 
<strong><em>What would be the best way to prevent concurrent execution of the tickForward() method?</em></strong></p>

<p>Some things I have tried or considered:</p>

<ul>
<li><p>Setting a Flag: When tickForward() runs, it sets an isRunning flag to true, and sets it back to false right before it ends. The logic for the event handler is set to only call tickForward() if isRunning is false. I tried a simple implementation of this, and isRunning never appeared to be changed.</p></li>
<li><p>The jQuery queue(): I think it would be useful to queue up the tickForward() commands, so if you clicked it five times quickly, it would still run as commanded but wouldn't run concurrently. However, in my cursory reading on the subject, it appears that a queue has to be attached to the object its queue applies to, and since my tickForward() method affects multiple lis, I don't know where I'd attach it.</p></li>
</ul>
