Skip to content
On this page

延时函数实现

setInterval实现setTImeout

js
function mySetTimeout(fn, delay){
    let timer = null;
    return (timer = setInterval(()=>{
        fn();
        clearInterval(timer)
    },delay));
}

setTImeout实现setInterval

js
function mySetInterval(fn, delay){
    let timer = null;
    const interval = function(){
        timer = setTimeout(()=>{
          fn();
          interval();
        }, delay);
    }
    interval();
    return {
        cancel:function(){
            clearTimeout(timer);
        }
    }
}

MIT Licensed | Copyright © 2021 - 2022