title: jQuery - function with a setInterval() being fired twice at once?
body: <p>I have a jQuery function that uses <code>setInterval()</code> to slowly type out a phrase--like with the phrase "string one", each character would be printed to the screen after a set number of milliseconds.</p>
  
  <p>The problem is that in my code, I have two calls to the function one right after the other, and they're overlapping because it appears that <code>setInterval()</code> doesn't stop the normal flow of code. It's hard to explain, <a href="http://jsfiddle.net/abluescarab/ymxu5/" rel="nofollow">here's my jsFiddle</a> that shows what happens.</p>
  
  <p>Here's my function:</p>
  
  <pre><code>function type(text) {
      if(intv == null) {
          var textArray = text.split("");
          var length = textArray.length - 1;
          var i = 0;
  
          $("#div").append('&lt;p class="text' + n + '"&gt;&lt;/p&gt;' + "\n"); // creates a paragraph to print every character to (n is a global var)
  
          intv = setInterval(function() {
              if(i &lt;= length) {
                  var a = textArray[i];
                  $('#div .text' + n).text($('#div .text' + n).text() + a); // appends the character to the paragraph created above
                  i++;
              }
              else {
                  alert("stopping interval..."); // just lets me know when the interval is stopping
                  clearInterval(intv);
                  intv = null;
                  n++;
              }
          }, 60);
      }
      else {
          alert("the interval is not null"); // lets me know if the previous setInterval has stopped firing
      }
  }
  </code></pre>
  
  <p>And here's how I'm calling it:</p>
  
  <pre><code>type("string one");
  type("string two");
  </code></pre>
  
  <p>The problem is that both "string one" and "string two" end up firing at the same time, and either start infinitely looping or only the first one works.</p>
  
  <p>I really don't know if there's a way to fix this, and my searches have yielded nothing. I don't know if this is a common problem or not. Is there a way that I can keep the second string from firing until the first one is done?</p>
  
  <p>Thanks ahead of time.</p>