lgwebdream / FE-Interview

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

定时函数 #1222

Closed opacitySun closed 3 years ago

opacitySun commented 3 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 3 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();