views:

37

answers:

2

This currently works:

$(document).ready(function() {
    $('body').addClass('red').delay(50).queue(function(next){
      $(this).removeClass('red').delay(50).queue(function(next2){
        $(this).addClass('blue');
        next2();
      });
      next();
    });

But I'd like something more efficient. And plus, the code above becomes a headache when I start adding and removing more classes...

+1  A: 

Take a look at jQuery's .toggleClass()-function.

EDIT: Try this:

var interval = setInterval(function() {
    $('body').toggleClass('red');
    if (/* some condition */) {
        clearInterval(interval); // abort
    }
}, 50);
elusive
Unfortunately, although it works, it's not the solution I'm looking for. :(
Dreadedkilla
Can you clearify that a bit? Do you want to add a class and remove it again after certain amounts of time?
elusive
What I really wanted was to add a class, remove it after a certain amount of time, and the cycle repeats.
Dreadedkilla
Then you need to do this in a continuous timeout. I edited my post accordingly.
elusive
A: 

use plain old setTimeOut() to setup intervals at every 50ms for as long as you want...

for(i=0;i<100;i++)setTimeout(blinkFunction, i*50);}
Quamis