写一个 mySetInterVal(fn, a, b),每次间隔 a,a+b,a+2b,...,a+nb 的时间,然后写一个 myClear,停止上面的 mySetInterVal

function mySetInterVal(fn,a,b) {
  let timer={};
  function setOneTimer(fn,a,b){
    timer.id=setTimeout(()=>{
      console.log(a);
      fn();
      setOneTimer(fn,a+b,b);
    },a)
  }
  setOneTimer(fn,a,b);
  return timer;
}
function myClear(timer){
  clearTimeout(timer.id);
}
const timer=mySetInterVal(()=>{console.log('run')},100,200);
setTimeout(()=>myClear(timer),2000);

posted @ 2021-06-11 17:33  有肌肉的小眼睛  阅读(159)  评论(0)    收藏  举报