亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

每秒隨機主體背景顏色

每秒隨機主體背景顏色

溫溫醬 2023-06-15 10:27:55
我制作了這個名為 makeRandColor 的函數,它使用 RGB 和模板字符串文字創建隨機顏色,但它一直困擾著我知道如何讓它每秒工作,我嘗試了 settime interval 但它似乎不起作用,我錯過了什么嗎?我想要它做的是這樣的, document.body.style.backgroundColor = makeRandColor; , 但每秒 settimeinterval
查看完整描述

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)


查看完整回答
反對 回復 2023-06-15
?
函數式編程

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);


查看完整回答
反對 回復 2023-06-15
  • 2 回答
  • 0 關注
  • 117 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號