title: javascript: how to exit a loop by clicking a button
body: <p>I did some searching and I'm not even sure if what I want to do is good javascript practice.
  I have a while loop that I would like to exit from early if a stop button is clicked.</p>
  
  <pre><code>$( "#go" ).click(function() {
    var gotime = 1;
    while (gotime &lt; 100) {
      for(i = 0; i &lt; 2; i++){
        var divName = "floatName" + i;
        console.log(divName);
        $( "#" + divName ).animate({
          left: Math.random()*500 + "px",
          top: Math.random()*500 + "px"
        }, 500, function() {
          // Animation complete.
        });
      };
  
      gotime += 1;
  
      $( "stop" ).click(function() {
        gotime = 101;
      });
      };
  });
  </code></pre>
  
  <p>This doesn't work though. I originally had an endless loop (not incrementing gotime).</p>
  
  <p><a href="http://jsfiddle.net/cvoxe465/" rel="nofollow">http://jsfiddle.net/cvoxe465/</a></p>