The below JavaScript changes the background color of a website randomly every five seconds. This code can be added to any page to create an interesting look for your website or individual web page.

Because this page uses the default Computer Hope template that already defines a background and background color see our example page for a demo. As you view the page, the background should automatically change every five seconds.

Source code

If five seconds is too long or too short to wait, the 5000 value can be adjusted. Also, pressing F5 to refresh the browser changes the background color each time the page refreshes.

  • For other color values that can be used see our HTML color code page.
  • How to change the background and text color of a web page.
  • See our JavaScript definition for further information, related links, and other examples.