JavaScript sleep睡眠函數(shù)的使用
JavaScript是單線程運(yùn)行的,沒(méi)有內(nèi)置的sleep函數(shù),現(xiàn)在模擬實(shí)現(xiàn)sleep延遲執(zhí)行的效果。
使用睡眠函數(shù)實(shí)現(xiàn)紅綠燈代碼,紅燈2秒,黃燈1秒,綠燈3秒,循環(huán)改變顏色。
2. setTimeout直接使用setTimeout實(shí)現(xiàn)sleep()的方法,兼容性最好,但是使用了回調(diào)函數(shù)的實(shí)現(xiàn)方式,代碼的可讀性和維護(hù)性不是很好。
// setTimeoutlet fun = () => console.log(’time out’);let sleep = function(fun,time){ setTimeout(()=>{ fun(); },time);}sleep(fun,2000);setTimeoutsetTimeout是最基本的實(shí)現(xiàn)方式,代碼如下,使用遞歸來(lái)實(shí)現(xiàn)循環(huán)改變顏色:function changeColor(color) { console.log(’traffic-light ’, color);}function main() { changeColor(’red’); setTimeout(()=>{ changeColor(’yellow’); setTimeout(() => { changeColor(’green’); setTimeout(main, 2000); }, 1000); }, 2000);}main();3.Promise
在ES6的語(yǔ)法中,Promise是sleep方法異步的實(shí)現(xiàn)一種方式,借助Promise方法可以優(yōu)雅的構(gòu)建sleep實(shí)現(xiàn)方法,避免了使用函數(shù)回調(diào)的使用方式。
// promiselet fun = () => console.log(’time out’);let sleep2= (time)=> new Promise((resolve)=>{ setTimeout(resolve,time)})sleep2(2000).then(fun);
Promise
使用Promise,把下一次的顏色改變寫(xiě)在then里面,最后同樣使用遞歸完成循環(huán)。
使用promise代替setTimeout,利用鏈?zhǔn)秸{(diào)用以及then來(lái)實(shí)現(xiàn)燈的轉(zhuǎn)換,then返回一個(gè)promise對(duì)象,當(dāng)這個(gè)對(duì)象為resolve狀態(tài)then可以持續(xù)調(diào)用。
const traffic_light=(color,duration)=>{ return new Promise((resolve,reject)=>{ console.log(’traffic-light ’, color); setTimeout(()=>{resolve() },duration) })}const main=()=>{ Promise.resolve() .then(()=>{return traffic_light(’red’,3000) }) .then(()=>{return traffic_light(’yellow’,1000) }) .then(()=>{return traffic_light(’green’,2000) }) .then(()=>{main(); })}main()4. async await
async await實(shí)際上是generator和promise的語(yǔ)法糖,在提供同步編程方式實(shí)現(xiàn)異步調(diào)用的基礎(chǔ)上,同時(shí)滿足對(duì)sleep函數(shù)語(yǔ)義化的支持,也是常用的sleep的實(shí)現(xiàn)方式。
// async awaitasync function wait(time){ await sleep2(time); fun();}wait(3000);
async await 使用
使用async await就可以避免Promise的一連串.then.then.then,也不再需要遞歸,使用while(true)就可以實(shí)現(xiàn)循環(huán)。
function sleep(duration) { return new Promise(resolve => { setTimeout(resolve, duration); })}async function changeColor(color, duration) { console.log(’traffic-light ’, color); await sleep(duration);}async function main() { while (true) { await changeColor(’red’, 2000); await changeColor(’yellow’, 1000); await changeColor(’green’, 3000); }}main();5. 1s后輸出1 2s后輸出2 3s后輸出3
const log = console.log;const sleep = (timeout) => { return new Promise((resolve)=>{ setTimeout(()=>{ resolve(); }, timeout) })}const main = async()=>{ await sleep(1000); log(1); await sleep(2000); log(2); await sleep(3000); log(3);}參考文章:
紅綠燈紅綠燈
到此這篇關(guān)于JavaScript sleep睡眠函數(shù)的使用的文章就介紹到這了,更多相關(guān)JavaScript sleep睡眠函數(shù)內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. python excel和yaml文件的讀取封裝2. moment轉(zhuǎn)化時(shí)間戳出現(xiàn)Invalid Date的問(wèn)題及解決3. python爬蟲(chóng)實(shí)戰(zhàn)之制作屬于自己的一個(gè)IP代理模塊4. idea重置默認(rèn)配置的方法步驟5. Android組件化和插件化開(kāi)發(fā)6. Python內(nèi)存映射文件讀寫(xiě)方式7. php實(shí)現(xiàn)當(dāng)前用戶在線人數(shù)8. Android Studio 2.0 功能介紹9. Ajax返回值類(lèi)型與用法實(shí)例分析10. Python中內(nèi)建模塊collections如何使用
