How to do a clear interval in javascript?
<p>My Code:</p>

<pre><code>$(function() {

     $('a.startSlideshow').click(function() {     
          startSlideShow = window.setInterval(function() { slideSwitch() }, 1000);
     });


     $('a.next').click(function() {     
          if (startSlideShow) {
               clearInterval(startSlideShow);
          }
          nextSlide();
     });

});
</code></pre>

<p>My Intention:</p>

<p>If a user clicks the startSlideshow link the slideshow function is executed every 1 seconds.</p>

<p>If the user clicks the next link, the slideshow function is stopped IF IT HAS ALREADY BEEN EXECUTED.</p>

<p>To be able to detect this, I stored the set interval as a variable. However when I check for it:</p>

<pre><code>          if (startSlideShow) {
               clearInterval(startSlideShow);
          }
</code></pre>

<p>It doesn't work. Right now my code works if I click on the start slideshow link before clicking next. But if the slideshow function was never started (i.e. I never clicked that link first but clicked next from the start) the code fails. </p>

<p>basically this is causing the code to break for some reason when startSlideshow var hasn't been defined:</p>

<pre><code>      if (startSlideShow) {
           clearInterval(startSlideShow);
      }
</code></pre>

<p>I did try other ways of checking to see if that variable has been set, but all failed. How to solve this?</p>
