Achieve effect
Implement a sleep()
delay function, for example, it sleep(2000)
means to wait for 2000
milliseconds before performing the operation in the function.
Implementation process
Use Promise
:
const sleep = (time) => {
return new Promise(resolve => setTimeout(resolve, time));
}
verification
console.log(1);
sleep(2000).then(() => {
console.log(2);
});
console.log(3);