任务是这样,定时器在0时刻打出1,然后隔1s打出2,隔2s打出3…….如此,时间间隔依次递增。
考虑用generator的 yield 功能来完成 停顿。
talk is cheap, here’s the code.
function sleep(sleepTime){
setTimeout( ()=>{
it.next() // 关键在于异步控制 iterator 的 下一步。每次都会在 yield 中 停顿。
}, sleepTime )
}
function *main(){
for(let i = 1; i < 10; i++){
console.log(i)
yield sleep