第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

具有不同時間的for循環(huán)中的setTimeout

具有不同時間的for循環(huán)中的setTimeout

慕的地8271018 2022-12-29 14:10:49
我有一個不同對象的數(shù)組,每個對象包含一個不同值的時間屬性。我想循環(huán)這個數(shù)組,里面有一個 setTimeout 函數(shù),時間將是每個對象的時間屬性。因此,我想要的結(jié)果是10 秒后,打印第一個對象的名稱20 秒后,打印第二個對象的名稱。......5 秒后,打印最后一個對象的名稱。但是,下面的代碼將累計執(zhí)行 20s,即 time = 5s 時打印 obj 3 & 5,5s 后打印 obj 1,10s 后打印 obj 2 & 4。const data = [{name: "Warm up", timeFormat: "00:10", time: 10},{name: "High interval", timeFormat: "00:20", time: 20},{name: "Low Interval", timeFormat: "00:05", time: 5},{name: "High interval", timeFormat: "00:20", time: 20},{name: "Low Interval", timeFormat: "00:05", time: 5},]function renderTimer(data) {  for (let i = 0; i < data.length; i++) {    const eachName = data[i].name;    const eachTime = data[i].time;    setTimeout(() => {      console.log(eachName);    }, eachTime * 1000);  }}renderTimer(data);我的代碼有什么問題?或任何其他方式來實現(xiàn)我想要的結(jié)果?非常感謝!
查看完整描述

4 回答

?
蝴蝶刀刀

TA貢獻1801條經(jīng)驗 獲得超8個贊

發(fā)生的事情是,程序在 for 循環(huán)中競速,并幾乎立即設(shè)置了相對于 t=0s 的超時。如果你想使用 setTimeout(),你必須自己累積時間:


const data = [

{name: "Warm up", timeFormat: "00:10", time: 10},

{name: "High interval", timeFormat: "00:20", time: 20},

{name: "Low Interval", timeFormat: "00:05", time: 5},

{name: "High interval", timeFormat: "00:20", time: 20},

{name: "Low Interval", timeFormat: "00:05", time: 5},

]


function renderTimer(data) {

  var timing = 0;

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

    const eachName = data[i].name;

    timing += data[i].time;


    setTimeout(() => {

      console.log(eachName);

    }, timing * 1000);

  }

}


renderTimer(data);


查看完整回答
反對 回復 2022-12-29
?
紫衣仙女

TA貢獻1839條經(jīng)驗 獲得超15個贊

你可以使用async/awaitPromises

它的工作原理是await new Promise(...)在 Promise 實現(xiàn)之前停止當前函數(shù)

const data = [

  {name: "Warm up", timeFormat: "00:10", time: 10},

  {name: "High interval", timeFormat: "00:20", time: 20},

  {name: "Low Interval", timeFormat: "00:05", time: 5},

  {name: "High interval", timeFormat: "00:20", time: 20},

  {name: "Low Interval", timeFormat: "00:05", time: 5},

]


async function renderTimer(data) {

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

    const eachName = data[i].name;

    const eachTime = data[i].time;


    await new Promise(res => {

      setTimeout(() => {

        console.log(eachName);

        // resolve the promise once the log is done

        res()

      }, eachTime * 100)}

    )

    // can't arrive here until the setTimeout is finalized

  }

}


renderTimer(data);


查看完整回答
反對 回復 2022-12-29
?
繁花如伊

TA貢獻2012條經(jīng)驗 獲得超12個贊

您也可以像這樣在遞歸函數(shù)中執(zhí)行此操作:


const data = [

{name: "Warm up", timeFormat: "00:10", time: 10},

{name: "High interval", timeFormat: "00:20", time: 20},

{name: "Low Interval", timeFormat: "00:05", time: 5},

{name: "High interval", timeFormat: "00:20", time: 20},

{name: "Low Interval", timeFormat: "00:05", time: 5},

]


const renderTimer = (data, i = 0) => {

  setTimeout(() => {

    console.log(data[i].name);

    

    // Call for next data

    if(i < data.length - 1)

      renderTimer(data, i + 1);

  }, data[i].time * 1000);

}


renderTimer(data);


查看完整回答
反對 回復 2022-12-29
?
哆啦的時光機

TA貢獻1779條經(jīng)驗 獲得超6個贊

你可以這樣做:


const data = [

{name: "Warm up", timeFormat: "00:10", time: 10},

{name: "High interval", timeFormat: "00:20", time: 20},

{name: "Low Interval", timeFormat: "00:05", time: 5},

{name: "High interval", timeFormat: "00:20", time: 20},

{name: "Low Interval", timeFormat: "00:05", time: 5},

]


var time = 0;


function renderTimer(data) {

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

    const eachName = data[i].name;

    time += data[i].time;

    const eachTime = time * 1000;


    setTimeout(() => {

      console.log(eachName);

    }, eachTime);

  }

}


renderTimer(data);


查看完整回答
反對 回復 2022-12-29
  • 4 回答
  • 0 關(guān)注
  • 142 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號