tags:

views:

47

answers:

4

Hi,

I have an animation on a partially hidden div container that will execute when the result returned from an ajax query is true. (The animation actually just brings the box into larger view, and then sliding it back in again)

$('#box').animate({'right':'-184px'}, 300, 'easeOutBounce');
$('#box').animate({'right':'-194px'}, 150, 'easeOutExpo');

How can I make this animation repeat every 4 seconds until the user clicks on $('#box')?

Any help is greatly appreciated. Thanks :)

Based on jAndy's and rahul's code..this is how I'm thinking of implementing it (fictitious setting).

This is the code that starts the animation:

// declares the variable first or $('#stop').click() will return undefined variable
var intervalId = '';
$('#submit').submit(function () {
  var intervalId = setInterval(function(){
    $('#box').animate({'right':'-184px'}, 300, 'easeOutBounce');
    $('#box').animate({'right':'-194px'}, 150, 'easeOutExpo');
  }, 4000);
});

This is the code that will supposedly will stop the animation:

$('#stop').click(function () {
  clearInterval(intervalId);
});
+2  A: 

You can use setInterval and then assign it to a variable. When the user clicks on the $("#box") button call clearInterval passing the variable as the parameter.

var intervalId = setInterval(function(){/*your code*/}, 4000);

$("#box").click(function(){
    clearInterval(intervalId);
});
rahul
You get the cookie for being first :) You almost lost if for the var name intervalid (what's an inter valid?) but I FTFY :)
Luke Schafer
since his only tag is jQuery and not 'javascript', I DESERVE IT :p /jk
jAndy
@rahul I'm getting a intervalId is not defined error..is it anything to do with the "var intervalId.." code going into another submit() event? Is there also an elegant way of making the animation start first (without waiting 4 seconds once it's first triggered?). Thanks for your help :)
Lyon
I declared the variable with var intervalId = ''; and it fixed the not defined error. But the clearInterval(intervalId) isn't stopping the animation..any ideas? Thanks
Lyon
if you use .delay() you could just append it after the last .animate() call
jAndy
You can use `.stop()` on `#box` after clearing the interval.
rahul
+1  A: 

use clearinterval or setinterval function of javascript for hiding dynamically created elements

check following link for more detail http://www.elated.com/articles/javascript-timers-with-settimeout-and-setinterval/

Pranay Rana
+1  A: 

Starting the timer:

var timer = window.setInterval(yourAnimationFunction, 4000);

Killing the timer:

$('#box').click(function(){
  clearInterval(timer);
});
Greg W
+3  A: 
function anibox(){
   $('#box').delay(4000).animate({'right':'-184px'}, {duration: 300, easing: 'easeOutBounce'});
   $('#box').animate({'right':'-194px'}, {duration: 150, easing: 'easeOutExpo', complete: anibox});
}

To start that animation just call

anibox()

anywhere in your code, to stop it use:

$('box').stop();

in your click event handler. That is the 'jQueryish' way, without setInterval().

Kind Regards

--Andy

jAndy
Thanks jAndy! I tried your code and the .stop() doesn't stop the animation. It just keeps going on and on. Any idea where it's going wrong?
Lyon
might be a typo.. $('#box').stop(); should do it
jAndy
@jAndy: i fixed that in the original code but it didn't stop. I updated my implementation of yours and rahul's code in my question. Is there any reason why it might not be working? How I applied your code was instead of `var intervalId....` i entered anibox(). Also, I replaced `clearInterval...` with $('#box').stop();
Lyon
example: http://jsbin.com/ekude3/4/edit
jAndy
Thank you jAndy! `$('#box').stop(true, true);` fixed it :) Many thanks again!
Lyon