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

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

如何取消對象內部的超時

如何取消對象內部的超時

哈士奇WWW 2023-06-15 17:21:38
我有以下代碼:const timers = []const timer1 = setTimeout(() => {console.log('Starting timer2')const timer2 = setTimeout(() => {  console.log('Its works')}, 10000)timers.push({key:2, id:timer2})}, 10000);timers.push({key:1, id:timer1})function remove(key) {  for (i = 0; i > timers.length; i++) {     if (timers[i].key === key) {     timers = timers.slice(i, 1)     clearTimeout(timers[i].id)  } }}當我調用 remove(key) 函數時,代碼沒有按預期刪除計時器
查看完整描述

1 回答

?
狐的傳說

TA貢獻1804條經驗 獲得超3個贊

const timers = []

const timer1 = setTimeout(() => {

console.log('Starting timer2')

const timer2 = setTimeout(() => {

  console.log('Its works')

}, 10000)

timers.push({key:2, id:timer2})

}, 10000);

timers.push({key:1, id:timer1})


function remove(key) {

   const timer = timers.find(f => f.key === key);

   if (timer) {

     clearTimeout(timer.id);

   }

}


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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