lgwebdream / FE-Interview

🔥🔥🔥 前端面试,独有前端面试题详解,前端面试刷题必备,1000+前端面试真题,Html、Css、JavaScript、Vue、React、Node、TypeScript、Webpack、算法、网络与安全、浏览器
https://lgwebdream.github.io/FE-Interview/
Other
6.76k stars 897 forks source link

定时函数 #1222

Closed opacitySun closed 2 years ago

opacitySun commented 2 years ago

function mySetInterVal(fn, a, b){ let ms = a; const timer = setInterval(() => { console.log(ms); fn(); ms += b; }, ms); return () => { if(timer) clearInterval(timer); } }

const myFn = mySetInterVal(() => { console.log(1111); }, 1000, 2000);

//清除轮询 myFn();

opacitySun commented 2 years ago

function mySetInterVal(fn, a, b){ let ms = a; const timer = setInterval(() => { console.log(ms); fn(); ms += b; }, ms); return () => { if(timer) clearInterval(timer); } }

const myFn = mySetInterVal(() => { console.log(1111); }, 1000, 2000);

//清除轮询 myFn();