Does anyone know why this only runs once? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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>

30th Apr 2018, 4:33 PM
Tyrell Reid
Tyrell Reid - avatar
2 Answers
+ 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.
30th Apr 2018, 4:38 PM
Zeke Williams
Zeke Williams - avatar
+ 1
Thanks, alot!
30th Apr 2018, 4:47 PM
Tyrell Reid
Tyrell Reid - avatar