间隔打印

本文最后更新于:7 个月前

function repeat(func, times, wait){
    return str =>{
        let i = 0;
        while(i<times) {
            setTimeout(()=>{
                func(str);
            },i*wait)
            i++;
        }
    }
} //实现打印4次,每次间隔 3000ms 
const repeatFun = repeat(console.log, 4, 3000);
repeatFun('Hello World')
function repeat(func, times, wait) {
    const delay = time => new Promise(resolve => setTimeout(resolve, time));
    return function(str) {
        let i = 0;
        while(i < times) {
            delay(wait * i).then(() => {
                func(str);
            })
            i++;
        }
    }
}

const repeatFun = repeat(console.log, 6,1000)
repeatFun("hello world");

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!