0
Does anyone know why this only runs once?
<script> "use strict"; var hex = ["a","b","c","d","e","f","A","B","C","D","E","F","1","2","3","4","5","6","7","8","9"]; var hexColor = hex[Math.floor(Math.random()*21)] + hex[Math.floor(Math.random()*21)] + hex[Math.floor(Math.random()*21)] + hex[Math.floor(Math.random()*21)] + hex[Math.floor(Math.random()*21)] + hex[Math.floor(Math.random()*21)]; var body = document.getElementsByTagName("body"); var colorChange = function() { body[0].style.backgroundColor = "#" + hexColor; } setInterval(colorChange,3000); </script>
2 Réponses
+ 1
Hex is not case sensitive. #aabb66 is the same color as #AABB66. Also, you're not setting the colors every interval change. Instead, you are setting the background color to the same color that was randomly picked. You need to move this:
var hexColor = hex[Math.floor(Math.random()*21)] + hex[Math.floor(Math.random()*21)] + hex[Math.floor(Math.random()*21)] + hex[Math.floor(Math.random()*21)] + hex[Math.floor(Math.random()*21)] + hex[Math.floor(Math.random()*21)];
into your function colorChange(). But make sure you pick the random color before setting the body background.
+ 1
Thanks, alot!