setTimeout: 倒计时后一次调用 setInterval: 间隔时间之间重复调用
const setTimeoutToInterval = ( fn, timeout ) => { const timer = { flag : true } ; const interval = ( ) => { if ( timer. flag) { fn ( ) ; setTimeout ( interval, timeout) ; } } ; setTimeout ( interval, timeout) ; return timer;
} ;
const fn = ( ) => console. log ( "------interval------" ) ;
const curTimer = setTimeoutToInterval ( fn, 1000 ) ;
setTimeout ( ( ) => { curTimer[ "flag" ] = false ;
} , 6000 ) ;
const _setTimeoutToInterval = ( fn, timeout, times ) => { if ( ! times) return ; setTimeout ( ( ) => { fn ( ) ; _setTimeoutToInterval ( fn, timeout, -- times) ; } , timeout) ;
} ;
const __setTimeoutToInterval = ( fn, timeout, times ) => { let timer = setTimeout ( function aa ( ) { fn ( ) ; timer-- ; timer = setTimeout ( aa, timeout) ; if ( times <= 0 ) { clearTimeout ( timer) ; } } , timeout) ;
} ;