简体   繁体   English

变色背景不会改变颜色

[英]color changing background won't change color

I'm working on a code for a game which requires a flashing background of random colors making use of Math.floor(), Math.random(), and arrays to create random hex codes.我正在为一个游戏编写代码,该代码需要使用 Math.floor()、Math.random() 和 arrays 来创建随机十六进制代码的随机 colors 背景闪烁。 I want the background to change color every 1 second, but the color doesn't even show up in the first place.我希望背景每 1 秒改变一次颜色,但颜色甚至不会首先出现。 Here's my code;这是我的代码;

<script>
var a = ["1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"];
var b = ["1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"];
var c = ["1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"];
var d = ["1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"];
var e = ["1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"];
var f = ["1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"];

var A;
var B;
var C;
var D;
var E;
var F;

setInterval(function(){
A = a[Math.floor(Math.random()*a.length)];
B = b[Math.floor(Math.random()*b.length)];
C = c[Math.floor(Math.random()*c.length)];
D = d[Math.floor(Math.random()*d.length)];
E = e[Math.floor(Math.random()*e.length)];
F = f[Math.floor(Math.random()*f.length)];
},1000);

document.body.style.backgroundColor = "#"+A+B+C+D+E+F;
</script>

If anyone could help, that would be great!如果有人可以提供帮助,那就太好了!

This line这条线

document.body.style.backgroundColor = "#"+A+B+C+D+E+F;

should go inside your setInterval应该 go 在你的setInterval

Ie IE

setInterval(function() {
  A = a[Math.floor(Math.random()*a.length)];
  B = b[Math.floor(Math.random()*b.length)];
  C = c[Math.floor(Math.random()*c.length)];
  D = d[Math.floor(Math.random()*d.length)];
  E = e[Math.floor(Math.random()*e.length)];
  F = f[Math.floor(Math.random()*f.length)];
  document.body.style.backgroundColor = "#"+A+B+C+D+E+F;
},1000);

you can access variables A, B, C, D, E, F inside your setInterval function, try this:您可以在 setInterval function 中访问变量 A、B、C、D、E、F,试试这个:

 var a = ["1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"]; var b = ["1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"]; var c = ["1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"]; var d = ["1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"]; var e = ["1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"]; var f = ["1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"]; setInterval(function(){ let A = a[Math.floor(Math.random()* a.length)]; let B = b[Math.floor(Math.random()* b.length)]; let C = c[Math.floor(Math.random()* c.length)]; let D = d[Math.floor(Math.random()* d.length)]; let E = e[Math.floor(Math.random()* e.length)]; let F = f[Math.floor(Math.random()* f.length)]; document.body.style.backgroundColor = '#' + A + B + C + D + E + F; }, 1000);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM