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 9152371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:01:33+00:00 2026-06-17T12:01:33+00:00

I am trying to make it so my websites background changes colour randomly every

  • 0

I am trying to make it so my websites background changes colour randomly every few seconds. I can get it to change to a random colour but how do I make it loop?

<script type="text/javascript">
$(document).ready(function() {

    var hue = 'rgb(' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ')';  

    function colourSet(){
       $("body").animate({ backgroundColor: hue}, 2000);
    }   

    colourSet();

});
</script>
  • 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-17T12:01:34+00:00Added an answer on June 17, 2026 at 12:01 pm

    You can make it loop simply by having the function register itself as its own completion handler:

    function colourSet() {
       var hue = 'rgb(' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ')'; 
       $("body").animate({ backgroundColor: hue}, 2000, colourSet);
    } 
    

    NB: the hue has to be picked inside the function otherwise it’ll remain the same colour every time.

    Just for fun, you can also make the hue code slightly shorter using some Javascript hacks (ahem):

    function r() { return ~~(256 * Math.random()) };
    function colourSet() {
       var hue = 'rgb(' + [r(), r(), r()] + ')';
       $("body").animate({ backgroundColor: hue}, 2000, colourSet);
    }  
    

    which takes advantage of two uses of the bitwise ~ (“not”) operator to convert floating point to integer, and also because concatenating an array to a string automatically does a .toString() on the array – resulting in a comma separated list.

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

Sidebar

Related Questions

I'm trying to make simple website with content background combined from 3 images: top
I'm trying to make a Google Maps widget for my website but all of
I'm trying to get data from a website- xml. Everything works fine. But the
I am trying to make a website for the students in my school but
I am trying to make a background to my website with a canvas. The
I am trying to get the background image of my content to appear behind
I'm trying to make a gradient background for my website, http://www.lathamcity.com The problem is,
I am trying to make a website with a video background using HTML5's video
I am trying to make it so my website can be viewed at any
I'm trying to make a full page DIV that holds all my content but

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.