26
rated 0 times
[
28]
[
2]
/ answers: 1 / hits: 49920
/ 13 Years ago, wed, november 23, 2011, 12:00:00
I'm trying to make a webpage change the background color every one second using JavaScript.
I'm using setTimeout
but I can't figure out how to get my variable to change in the function. Here's my code:
<!DOCTYPE html>
<html>
<head>
<script type=text/javascript>
function changecolors() {
x = 1; // <-- I know this is wrong, I just don't know where to place it
var t = setTimeout(change(), 1000);
}
function change() {
while(x < 3) {
if(x = 1) {
color = red;
x++;
} else if (x = 2) {
color = green;
x = 1;
}
document.body.style.background = color;
}
}
</head>
<body onload=changecolors()>
</body>
</html>
More From » html