如何在js中每5秒更改一次矩形的颜色

时间:2016-02-08 04:23:52

标签: javascript html

此代码更快地更改矩形的颜色。如何更改它以使颜色每5秒钟改变一次?

var bgcolorlist=new Array("#DFDFFF", "#FFFFBF", "#80FF80", "#EAEAFF", "#C9FFA8", "#F7F7F7", "#FFFFFF", "#DDDD00")
POP.Draw.rect(0, 0, POP.WIDTH, POP.HEIGHT, bgcolorlist[Math.floor(Math.random()*bgcolorlist.length)]);

2 个答案:

答案 0 :(得分:0)

Please try this 

var bgcolorlist=new Array("#DFDFFF", "#FFFFBF", "#80FF80", "#EAEAFF","#C9FFA8", "#F7F7F7", "#FFFFFF",     "#DDDD00")
function draw(){
var canvas = document.getElementById('a');
var context = canvas.getContext('2d');
context.beginPath();
context.rect(20,20, 100, 100);
context.fillStyle =bgcolorlist[Math.floor(Math.random()*bgcolorlist.length)];
context.fill();
context.lineWidth = 7;
}
draw();
setInterval(function(){
draw();
},5000);

答案 1 :(得分:0)

的setInterval(函数(){ object.style.backgroundColor = bgcolorlist [Math.floor(的Math.random()* bgcolorlist.length)]; },5000);