這篇文章主要為大家展示了“js中async函數(shù)結(jié)合promise的示例分析”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“js中async函數(shù)結(jié)合promise的示例分析”這篇文章吧。
效果圖
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> *{ margin: 0; padding: 0; list-style: none; } .box{ height: 15px; margin-top: 20px; transform: translateX(-100%); transition: all 1s linear; } button{ margin-top: 30px; } </style> </head> <body> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <button id="btn">點擊開始</button> <script> const box=document.querySelectorAll(".box") box.forEach((item,index)=>{ item.style.cssText=`width:${100+index*15}px;background:rgb(${Math.random()*255},${Math.random()*255},${Math.random()*255})` }) btn.onclick=async function(){ for(let i=0;i<box.length;i++){ await fn(box[i]) } } function fn(el){ return new Promise((resolve,reject)=>{ el.style.transform=`translateX(0)`; el.addEventListener("transitionend",function(){ resolve();//為什么調(diào)用這個成功函數(shù) }) }) } </script> </body> </html>
以上是“js中async函數(shù)結(jié)合promise的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
當(dāng)前文章:js中async函數(shù)結(jié)合promise的示例分析-創(chuàng)新互聯(lián)
本文地址:http://jinyejixie.com/article12/icegc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗、關(guān)鍵詞優(yōu)化、品牌網(wǎng)站建設(shè)、網(wǎng)站維護、全網(wǎng)營銷推廣、移動網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容