2 回答

TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超5個(gè)贊
你必須像這樣使用它:
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},$)`
}
setInterval(() => {
document.body.style.backgroundColor = makeRandColor();
},1000)

TA貢獻(xiàn)1807條經(jīng)驗(yàn) 獲得超9個(gè)贊
示例解決方案:
const changeBackground = (element) => {
const random0To255 = () => Math.floor(Math.random() * 255);
return () => {
element.style.backgroundColor = `rgb(${
random0To255()}, ${
random0To255()}, ${
random0To255()})`;
}
};
setInterval(changeBackground(document.querySelector('body')), 1000);
添加回答
舉報(bào)