主题
sleep
延迟指定时间后解析的 Promise
引入
js
import { sleep } from '@/uni_modules/lime-shared/sleep'
示例
js
// 示例1:使用默认延迟时间(300毫秒)
async function example1() {
console.log('开始延迟');
await sleep(); // 使用默认的300毫秒延迟
console.log('延迟结束');
}
example1();
// 示例2:指定延迟时间为1000毫秒(1秒)
async function example2() {
console.log('开始1秒延迟');
await sleep(1000); // 指定1000毫秒延迟
console.log('1秒延迟结束');
}
example2();
// 示例3:在异步函数中使用sleep来暂停执行
async function example3() {
for (let i = 0; i < 5; i++) {
console.log(`计数:${i}`);
await sleep(500); // 每500毫秒打印一次计数
}
}
example3();