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

  • Home
  • SEARCH
  • 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 6871973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:52:08+00:00 2026-05-27T03:52:08+00:00

http://jsfiddle.net/JamesKyle/re73T/ Trying to make a class switcher that will do the following: On Load

  • 0

http://jsfiddle.net/JamesKyle/re73T/

Trying to make a class switcher that will do the following:

  1. On Load – randomly select one of 10 classes to add to an element
  2. While Hovering – Rapidly (every 0.4s) switch between 10 classes
  3. On mouseOut – leave the current class active

I’ve tried several ways of doing this, none of which have worked.

What do you think would be the best way to do this?

  • 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-05-27T03:52:09+00:00Added an answer on May 27, 2026 at 3:52 am

    Here’s a different implementation that works in your jsFiddle here: http://jsfiddle.net/jfriend00/b7A4a/. This one also makes sure that the randomly generated class name is different than the previous one so the color actually changes every 400ms rather than skipping a change 1 out of 10 times because it generates the same color value that it just had.

    // get unique random class that's different than what is currently being used
    function getRandomClass(prior) {
        var r;
        do {
            r = "class" + (Math.floor(Math.random() * 10) + 1); 
        } while (prior && prior.match(new RegExp("\\b" + r + "\\b")));
        return(r);
    }
    
    // initialize the current class
    $(".element").addClass(getRandomClass());
    
    // upon hover, start the timer, when leaving stop the timer
    $(".element").hover(function() {
        var self = this;
        if (this.intervalTimer) {
            clearInterval(this.intervalTimer);
            this.intervalTimer = null;
        }
        this.intervalTimer = setInterval(function() {
            var c = self.className;
            self.className = c.replace(/\bclass\d+\b/, getRandomClass(c));
        }, 400);
    }, function() {
        clearInterval(this.intervalTimer);
        this.intervalTimer = null;
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

http://jsfiddle.net/JamesKyle/y7hBQ/ I'm making a simple jquery counting function that adds the following classes: First
I need help concerning following code: http://jsfiddle.net/8PsdE/ <SELECT ID=pizzasize NAME=pizzasize> <OPTION VALUE=s>small <OPTION VALUE=m>medium
http://jsfiddle.net/jasondavis/Mt87G/1/ I am trying to do an un-ordered list, each list item will have
http://jsfiddle.net/yxhzU/1042/ I'm trying to modify this example so that within the top carousel you
http://jsfiddle.net/nicktheandroid/B7Rhe/5/ I'm trying to make the next slide fade in over the current slide,
http://jsfiddle.net/xysxD/11/ if you check the jsfiddle Will have a comment text that slide toggle
http://jsfiddle.net/zjRga/55/ I want a div to open up that will appear over top of
In the following link: http://jsfiddle.net/gCvbT/ , if you click on General and then click
Even though the following works: http://jsfiddle.net/N7D5r/ My attempt at using the same code does
I have the following code: http://jsfiddle.net/SPWWx/ I'm completely new to javascript, this is my

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.