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以便調用者可以存儲它并在以后需要時清除它。

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