Reputation: 15
I'm trying to make a repeating animation using jQuery. I used a while
loop with the condition set to true
, but I think the browser doesn't like it.
What is the way to do this properly?
$('.test').click(function () {
while (true) {
$(this).hide(1000);
$(this).show(1000);
}
});
Upvotes: 0
Views: 89
Reputation: 28554
You'll need to write a function that has itself as a callback. That's how you loop a function.
$('.test').click(fadeIt);
function fadeIt() {
$('.test').hide(1000, function() {
$('.test').show(1000, fadeIt);
});
}
Upvotes: 0
Reputation: 33880
This should be enough :
$('.test').click(doMe);
function doMe(){
$(this).hide(1000).show(1000, doMe);
}
jQuery queue animation for you.
Upvotes: 2
Reputation: 208032
function h() { $(this).hide(1000, s); };
function s() { $(this).show(1000, h); };
$('.test').click(h);
Or, without the width/height animation that hide/show creates when specifying a duration so you get more of a blink effect: jsFiddle example
Upvotes: 0