Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sleep实现 #15

Open
nianxiongdi opened this issue Sep 25, 2019 · 0 comments
Open

sleep实现 #15

nianxiongdi opened this issue Sep 25, 2019 · 0 comments

Comments

@nianxiongdi
Copy link
Owner

方式1

async function sleep(delay) {
    return new Promise((resolve, reject)=>{
        setTimeout(function(){}, delay);
        resolve('success delay');
    });
}

const start = Date.now();
(async function(){
    await sleep(2000);
})()
const end = Date.now();

方式2

function sleep(delay){
    const start = Date.now();
    while(Date.now() - start <delay){}
}

sleep(2000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant