163
rated 0 times
[
167]
[
4]
/ answers: 1 / hits: 12902
/ 10 Years ago, sun, october 19, 2014, 12:00:00
I'm trying to make div which could change it color. Here is my code:
window.onload = function () {
for (;;){
setTimeout(function () {
document.querySelector('style').style.backgroundColor = colors[rand(colors.length)];
}, 2000);
}
}
var colors = [
'red',
'green',
'blue',
'yellow',
'magenta',
'pink'
];
var rand = function (max) {
return Math.floor(Math.random() * max);
};
.style{
background-color: pink;
top: 50px;
left: 50px;
height: 50px;
width: 50px;
}
<body>
<div class=style></div>
</body>
But I can't find out why it doesn't work.
EDIT: the script also crashes the browser
More From » javascript