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

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

我的功能有問題:discord js 的 updateChannel

我的功能有問題:discord js 的 updateChannel

肥皂起泡泡 2022-01-13 15:42:04
我希望您就我創建的一個功能尋求幫助,該功能允許刷新列表中的頻道,但我有一個問題,每 10 秒內存只會增加一次,而且她永遠不會清空自己。找了5個多小時,即使我認為這是一個愚蠢的事情,提前謝謝你的幫助(對不起翻譯,我不是英語)我的代碼:updateChannel: async function(client, newList){    let a = setInterval(async ()=> {        for(let i = 0; i < newList.length; i++){            const message = await this.replace$Var(client, newList[i][1])            const channel = await client.channels.get(newList[i][0])            channel.setName(message).catch(err => {                console.log(`The channel with the id : ${newList[i][0]} was deleted, please restart the bot`)                newList.splice(i,1)                i-=1            })        }        clearInterval(a)        this.updateChannel(client, newList)    }, 10000)}
查看完整描述

2 回答

?
守著一只汪

TA貢獻1872條經驗 獲得超4個贊

從您使用它的方式來看,您根本不需要遞歸,并且像這樣未經檢查的無限遞歸只會導致內存問題,因為節點會創建越來越多的堆棧幀并捕獲變量。


嘗試不遞歸地編寫它:


updateChannel: function(client, newList) {

    return setInterval(async ()=> {

        for(let i = 0; i < newList.length; i++) {

            const message = await this.replace$Var(client, newList[i][1])

            const channel = await client.channels.get(newList[i][0])

            channel.setName(message).catch(err => {

                console.log(`The channel with the id : ${newList[i][0]} was deleted, please restart the bot`)

                newList.splice(i,1)

                i-=1

            })

        }

    }, 10000)

}

我返回返回值,setInterval以便調用者可以存儲它并在以后需要時清除它。


查看完整回答
反對 回復 2022-01-13
?
慕雪6442864

TA貢獻1812條經驗 獲得超5個贊

不要以這種方式使用 setInterval。使用設置超時。通過調用 setInterval,您可以在每次調用該函數時創建一個 UNIQUE 計時器。SetTimeout 將創建一個結束的計時器,然后創建一個新的計時器。


嘗試這樣的事情:


updateChannel: async function(client, newList){

  for (let i = 0; i < newList.length; i++) {

    const message = await this.replace$Var(client, newList[i][1])

    const channel = await client.channels.get(newList[i][0])

    channel.setName(message).catch(err => {

      console.log(`The channel with the id : ${newList[i][0]} was deleted, please restart the bot`)

      newList.splice(i, 1)

      i -= 1

    })

  }

  setTimeout(this.updateChannel , 100000, client, newList);

}


查看完整回答
反對 回復 2022-01-13
  • 2 回答
  • 0 關注
  • 188 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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