Using .queue()

Using .queue()

Hello !

I'm using queue to do a non stop animation (a blink animation), but when I try to stop it, I'd like that the animation runs until the stack end and not stop immediatly.

My code :

  1. <html>
  2. <head>
  3.     <script src="http://code.jquery.com/jquery-latest.min.js"></script>
  4.     <style>
  5.         div {
  6.             position: absolute;
  7.             top: 0px;
  8.             left: 0px;
  9.             width: 200px;
  10.             height: 200px;
  11.             border: 2px black solid;
  12.             background-color: green;
  13.         }
  14.     </style>
  15. </head>
  16. <body>
  17.     <input type="button" value="Stop it!" />
  18.     
  19.     <div id="foo"><p>Hello</p></div>
  20.     
  21.     <script type="text/javascript">
  22.         var blink = function(next) {
  23.             $("div").animate({left: '+=500px'}, 2000);
  24.             $("div").animate({left: '-=500px'}, 2000, blink);
  25.             
  26.             if($.isFunction(next)) {
  27.                 next();
  28.             }
  29.         };

  30.         $("div").queue('blink', blink);
  31.         $("div").dequeue("blink");
  32.         
  33.         $("input").click(function() {
  34.             $("div").stop();
  35.         }); 
  36.     </script>
  37. </body>
  38. </html>

My question is : how to stop the animation when the div is on his initial position ?

Thanks