代码如下
async function delay(time) { return new Promise((resolve, reject) => { setTimeout(() => { resolve(); }, time); }); } // 示例:延迟1秒 输出 (async () => { await delay(1000); console.log("hello!"); })();
可以使用npm的开源库:https://www.npmjs.com/package/delay
代码如下
async function delay(time) { return new Promise((resolve, reject) => { setTimeout(() => { resolve(); }, time); }); } // 示例:延迟1秒 输出 (async () => { await delay(1000); console.log("hello!"); })();
可以使用npm的开源库:https://www.npmjs.com/package/delay