tags:

views:

59

answers:

1

i'm using jquery to pulsate text. all fine - but i can't get my head around something: i would only like to pulsate x-number of times and then stop. i'm using the folling code to pulsate a class:

$(document).ready(function() {
  function pulsate() {
    $(".pulsate").
      animate({opacity: 0.2}, 1000, 'linear').
      animate({opacity: 1}, 1000, 'linear', pulsate);
  }
  pulsate();
});

Any ideas how this can be achived? probably one line of code...?!

thanks, mark

+1  A: 

The simplest way is to just count:

$(document).ready(function() {
  var i = 0;
  function pulsate() {
    if(i >= 3) return;
    $(".pulsate").
      animate({opacity: 0.2}, 1000, 'linear').
      animate({opacity: 1}, 1000, 'linear', pulsate);
    i++;
  }
  pulsate();
});

Try it here. Or, queue up all the animations at once in a for loop, like this:

$(function() {
  var p = $(".pulsate");
  for(var i=0; i<3; i++) {
    p.animate({opacity: 0.2}, 1000, 'linear')
     .animate({opacity: 1}, 1000, 'linear');
  }
});

Try that version here.

Nick Craver