mercredi 15 mars 2017

Change the background color of an element inside `setInterval`

I'm trying to make an <h1> element's color change every 300 ms by using document.getElementById("h1").style and making it a variable that makes a random color, but it doesn't seem to be working.

Here's my code:

function getRandomColor() {
    var letters = '0123456789ABCDEF';
    var color = '#';
    for (var i = 0; i < 6; i++ ) {
        color += letters[Math.floor(Math.random() * 16)];
    }
    return color;
}
var newColor = getRandomColor();
function color() {
    document.getElementById("h1").style = "backgroundColor = " + newColor;
    setTimeout(color(), 300)
}




Aucun commentaire:

Enregistrer un commentaire