您的当前位置:首页正文

js手写实现sleep函数?

2024-11-11 来源:个人技术集锦
const sleep=(t)=>{
    return new Promise(resolve=>setTimeout(resolve,t))
}

function test(){
    console.log('1');
    sleep(1000).then(()=>{
        console.log('1秒后输出的内容');
    })
}

function sleep(time) {
    const startDate = (new Date()).getTime()
    while((new Date()).getTime() - startDate < time) {
        continue;
    }
}

function test(){
    console.log('111');
    sleep(3000);
    console.log('222');
    console.log(`sleep exe ending!`);
}

 

 

Top