Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8499009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:33:19+00:00 2026-06-11T00:33:19+00:00

Possible Duplicate: jquery add a fade to an .addClass Here is my jsFiddle example:

  • 0

Possible Duplicate:
jquery add a fade to an .addClass

Here is my jsFiddle example: http://jsfiddle.net/sLzGc/

I am trying to create an effect where it appears options 1 through 3 are being rotated. Every 1.2 seconds the green class is removed from the option that has it and the green class is added to the next option. The idea is that the rotation through the three options happens five times (i<5) before the final function alert('Finished') is executed.

I’m trying to use setTimeout(), but I’m open to using .delay(), another jQuery function, or some other option.

Here is my JavaScript/jQuery:

alert('Start');

sampleFunction();

function sampleFunction() {

for (i=0; i<5; i++) {

  setTimeout(function() {
    $('#option2').addClass('green');
    $('#option1').removeClass('green');
  },1200);

  setTimeout(function() {
    $('#option3').addClass('green');
    $('#option2').removeClass('green');
  },1200);

  setTimeout(function() {
    $('#option1').addClass('green');    
    $('#option2').removeClass('green');
  },1200);

}//end for loop

}

alert('Finished');​

My whole example is on jsFiddle: http://jsfiddle.net/sLzGc/

Thanks for any help or insight you can offer!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T00:33:20+00:00Added an answer on June 11, 2026 at 12:33 am

    Here’s how I would approach the problem:

    ​(​function() {
        var options = [$("#option1"), $("#option2"), $("#option3")],
            cycles = 5,
            i = 0,
            $last = $(null);
    
        (function next() { 
            if(i < cycles * options.length) {
                $last.removeClass("green");
                $last = options[i % options.length].addClass("green");
                i++;
                setTimeout(next, 1200);
            } else {
                $last.removeClass("green");
                alert("finished!"); //do any other "we're done" stuff here.
            }
        }());
    }());​
    

    It’s pretty straightforward and the cool part is that you can add elements to the list of elements to cycle through pretty easily (just add them to options). You can also set the number of cycles if you want. I didn’t make the class or delay length a variable, but that would be very simple in case you wanted to do that.

    You can see a demo here:

    http://jsfiddle.net/D7SR8/

    You might also consider having a callback function at the end and putting the alert() in there. That would be more reusable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: jQuery Youtube URL Validation with regex YouTube url id Regex http://www.youtube.com/watch?v=SCS7SIeF30E http://www.youtube.com/watch?v=SCS7SIeF30E&feature=relmfu
Possible Duplicate: jQuery Ajax always returns “undefined”? I'm trying to come up with a
Possible Duplicate: jquery validate & ajax.beginform I'm trying to use the jQuery validate plugin
Possible Duplicate: jQuery question: what does it really mean? For example, (function (exports, $,
Possible Duplicate: What does jQuery.fn mean? I have some script on a page trying
Possible Duplicate: Jquery Add Values to Select Options How can I empty a dropdown,
Possible Duplicate: jQuery HTML5 Speech Input I want create an search engine for my
Possible Duplicate: In Javascript, can you extend the DOM? I'm trying to add methods
Possible Duplicate: jQuery: how to add <li> in an existing <ul>? I have a
Possible Duplicate: jQuery convert line breaks to br (nl2br equivalent) Currently I add <BR>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.