setinterval change background color code example

Example 1: javascript change background color setinterval

var i = 0;
function change() {
  var doc = document.getElementById("background");
  var color = ["black", "blue", "brown", "green"];
  doc.style.backgroundColor = color[i];
  i = (i + 1) % color.length;
}
setInterval(change, 1000);

Example 2: javascript change background color setinterval

<HTML>
<HEAD>
<TITLE>Using setInterval()</TITLE>
<SCRIPT LANGUAGE="JavaScript1.2"><!--
colors = new Array("red","orange","green","blue","brown","purple","gray","white")

colorIndex = 0

function changeColor() {
 document.bgColor = colors[colorIndex]
 colorIndex = (colorIndex + 1) % colors.length
}

function startColorChange() {
 setInterval("changeColor()",3000)
}

window.onload = startColorChange
// --></SCRIPT>

</HEAD>
<BODY BGCOLOR="white">

<P>The setInterval() repeatedly changes the 
document background color every three seconds.</P>
</BODY>
</HTML>

Tags:

Misc Example