简单延迟几秒
function delay(seconds) {
return new Promise(resolve => setTimeout(resolve, seconds * 1000));
}
// 使用这个函数来延迟1秒
delay(1).then(() => {
console.log('1秒已经过去了!');
});
简化版
function delay(seconds) {
return new Promise(r => setTimeout(r, seconds * 1000));
}
// 使用这个函数来延迟1秒
delay(1).then(() => {
console.log('1秒已经过去了!');
});
延迟和回调函数
function delay(seconds,cb) {
return new Promise(resolve => setTimeout(resolve, seconds * 1000));
}
// 使用这个函数来延迟1秒
delay(1).then(() => {
console.log('1秒已经过去了!');
});
### 为什么使用,主要是调用很方便
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
delay(1000)
.then(() => console.log('一秒钟后输出'))
.then(() => delay(2000))
.then(() => console.log('再过两秒后输出'));
标签:resolve,console,log,实现,delay,seconds,promise,延迟
From: https://www.cnblogs.com/cn-oldboy/p/17980852