Created
May 3, 2019 07:35
-
-
Save abearxiong/7902dd0fd4391374cc131ae37c0f9894 to your computer and use it in GitHub Desktop.
js sleep
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function sleep(ms){ | |
return new Promise((resolve)=>setTimeout(resolve,ms)); | |
} | |
async function test(){ | |
var temple=await sleep(1000); | |
console.log(1111) | |
return temple | |
} | |
test(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 利用循环 | |
function sleep(d){ | |
for(var t = Date.now() ; Date.now()-t <= d ;) | |
{ | |
continue; | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function* sleep(ms){ | |
yield new Promise(function(resolve,reject){ | |
console.log(111); | |
setTimeout(resolve,ms); | |
}) | |
} | |
sleep(500).next().value.then(()=>{ | |
console.log(11111) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var sleep = require('sleep'); | |
var n=10; | |
sleep.sleep(n) //sleep for n seconds | |
sleep.msleep(n) //sleep for n miliseconds | |
sleep.usleep(n) //sleep for n microseconds (1 second is 1000000 microseconds) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function sleep(ms) { | |
return new Promise(resolve => | |
setTimeout(resolve, ms) | |
) | |
} | |
sleep(3000).then(()=>{ | |
//code | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment