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},$)`
}
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);
添加回答
舉報