2 回答

TA贡献1824条经验 获得超5个赞
你必须像这样使用它:
const makeRandColor = () => {
const r = Math.floor(Math.random() * 255)
const g = Math.floor(Math.random() * 255)
const b = Math.floor(Math.random() * 255)
return `rgb(${r},${g},${b})`
}
setInterval(() => {
document.body.style.backgroundColor = makeRandColor();
},1000)

TA贡献1807条经验 获得超9个赞
示例解决方案:
const changeBackground = (element) => {
const random0To255 = () => Math.floor(Math.random() * 255);
return () => {
element.style.backgroundColor = `rgb(${
random0To255()}, ${
random0To255()}, ${
random0To255()})`;
}
};
setInterval(changeBackground(document.querySelector('body')), 1000);
添加回答
举报