Create a random website background color every five seconds
Updated: 01/31/2019 by Computer Hope
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.
Tip
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.
Source code
<script type="text/javascript"> // // Description: Randomly change background color every 5 seconds // // NewcWare 1997 // Author: Scott Newcomer 3/1997 // Email: [email protected] // function setbackground() { window.setTimeout( "setbackground()", 5000); // 5000 milliseconds delay var index = Math.round(Math.random() * 9); var ColorValue = "FFFFFF"; // default color - white (index = 0) if(index == 1) ColorValue = "FFCCCC"; //peach if(index == 2) ColorValue = "CCAFFF"; //violet if(index == 3) ColorValue = "A6BEFF"; //lt blue if(index == 4) ColorValue = "99FFFF"; //cyan if(index == 5) ColorValue = "D5CCBB"; //tan if(index == 6) ColorValue = "99FF99"; //lt green if(index == 7) ColorValue = "FFFF99"; //lt yellow if(index == 8) ColorValue = "FFCC99"; //lt orange if(index == 9) ColorValue = "CCCCCC"; //lt grey document.getElementsByTagName("body")[0].style.backgroundColor = "#" + ColorValue; } </script> <body onload="setbackground();">