//1.普通方法
function delay(time) {
new Promise(resolve => {
setTimeout(() => {
resolve()
}, time)
})
}
await delay(2000)
console.log('test')
//2.箭头函数方法
const delay = (time) => new Promise(resolve => setTimeout(() => resolve(), time))
await delay(2000)
//3.舍弃函数封装与自定义时长,最精简写法
(async () => {
await new Promise(r => setTimeout(() => r(), 2000)) // 一行代码
console.log('test')
})()
/** vue中的用法 */
main.js全局挂载
vue.prototype.$delay = (t) => new Promise(r => setTimeout(() => r(), t))
await this.$delay(2000)