Lion King
Lion King

Reputation: 33813

Change the background color of page continually does not work

I want to change the background color of page continually.
I have created a code to do what I want, but doesn't work on modern browsers like ff, ch, IE9+.
Also there is no any error message appear.

The code:

function randColor(colorPackage) {
    var color = "", len = colorPackage.length - 1;
    for (var i = 0; i < 6; i++) {
        color += colorPackage[Math.ceil(Math.random() * len)];
    }
    return color;
}

var package = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'a', 'b', 'c', 'd', 'e', 'f'];
setInterval(function () {
    document.body.style.backgroundColor = "'#" + randColor(package) + "'";
}, 1000);

Upvotes: 0

Views: 45

Answers (2)

NikzJon
NikzJon

Reputation: 954

Change the following code:

document.body.style.backgroundColor = "'#" + randColor(package) + "'";

to

document.body.style.backgroundColor = "#" + randColor(package);

Check this for more details on the usage of the property.

Upvotes: 1

j08691
j08691

Reputation: 207900

You have too many quotes in your style line. Change:

document.body.style.backgroundColor = "'#" + randColor(package) + "'";

to:

document.body.style.backgroundColor = "#" + randColor(package);

jsFiddle example

Upvotes: 3

Related Questions