Search code examples
javascriptjqueryaddclassremoveclassblink

jquery blink a few


i want to give blink effect to an element by using addClass and removeClass 3 times each i tried this

$("#div").addClass("orange").delay(300).queue(function(next){
    $(this).removeClass("orange");
    next();
});

this is works just 1 time

how can i make this happened 3 times with 300 ms delay?


Solution

  • Just count to three:

    (function() {
      var count = 0, $div = $('#div'), interval = setInterval(function() {
        if ($div.hasClass('orange')) {
          $div.removeClass('orange'); ++count;
        }
        else
          $div.addClass('orange');
    
        if (count === 3) clearInterval(interval);
      }, 300);
    })();
    

    You could get fancy and write your own animation plugin for it, I guess.