作者ggbast (GG)
看板Ajax
標題[問題] 如何在迴圈中延遲?
時間Thu Jul 5 16:50:50 2018
最近剛踏入javascript的領域
我想在一個迴圈中重複執行某一個function
這個function是個同步函數
我希望每次都做完這個function才延遲
大概像是下面這樣
while (cnt<999) {
function()
delay()
cnt++
}
我試過了setTimeout 結果跟預期的不同
用了promise跟then的作法也是不如預期
請問各位大神有什麼方法可以做到我要的效果呢?
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 49.218.50.232
※ 文章網址: https://webptt.com/m.aspx?n=bbs/Ajax/M.1530780652.A.8D6.html
2F:→ chu2815: pt/Guide/Iterators_and_Generators07/05 18:30
4F:推 sckm160913: new Promise(r => setTimeout(r, 5000))07/05 21:40
5F:→ sckm160913: 還要await07/05 21:42
6F:→ y3k: async ()=>07/06 13:42
7F:→ xdraculax: 這情況用 setInterval 比 setTimeout 好點07/07 10:00
感謝幫忙解答的各位:)
※ 編輯: ggbast (49.214.51.121), 07/09/2018 09:58:39