You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Leolijiaming, littletortoisesmile, ckhesy, witimlfl, wybiubiu1012 and 4 morefanzye95th, cuzmarlboro, Kailnese, vyeh852, jiadaoyun and 2 morexqfd007 and liangzuishuaitiantiandaboli, hexu107 and tangjiangxi
function mySetInterVal(fn, a, b) {
let timeCount = 0;
let timer
const loop = () => {
timer = setTimeout(() => {
fn()
timeCount++
loop()
}, a + timeCount * b)
}
loop()
return () => {
clearTimeout(timer)
}
}
//测试
const myClear =mySetInterVal(()=>{console.log('test')},1000,500);
// 清除定时器
myClear()
Pxygogogo, cYang2030, Freeruning, liulei92, DaDiaoShuai and 68 morehoudiniping and 814384276lang711 and 814384276houdiniping and 814384276Freeruning, sinSquid, huxiaocheng, re-doc, o-only-one and 6 morehoudiniping and 814384276
function mySetInterVal(fun, a, b) {
!mySetInterVal.prototype.maxLimit && (mySetInterVal.prototype.maxLimit = a + 2 * b)
if (a > mySetInterVal.prototype.maxLimit) {
mySetInterVal.prototype.maxLimit = null
return
}
return setTimeout(() => {
mySetInterVal(fun, a + b, b)
fun()
}, a);
}
function myClear(timeId) {
clearTimeout(timeId)
mySetInterVal.prototype.maxLimit = null
winslow-demo and Rednaxela-2winslow-demo and Rednaxela-2winslow-demo and Rednaxela-2winslow-demo and Rednaxela-2winslow-demo and Rednaxela-2Rednaxela-2
function mySetInterval(fn, a = 1000, b = 1000) {
let count = 0
setTimeout(interval, a)
function interval() {
fn(setTimeout(interval, a + b * ++count))
}
}
function myClear(id) {
clearTimeout(id)
}
// 测试
let count = 0
mySetInterval((timer) => {
console.log('test');
count++
if (count >= 5) {
myClear(timer)
}
}, 1000, 1000)
------------------ 原始邮件 ------------------
发件人: ***@***.***>;
发送时间: 2024年9月6日(星期五) 下午4:13
收件人: ***@***.***>;
抄送: ***@***.***>; ***@***.***>;
主题: Re: [lgwebdream/FE-Interview] 第 1 题:写一个 mySetInterVal(fn, a, b),每次间隔 a,a+b,a+2b,...,a+nb 的时间,然后写一个 myClear,停止上面的 mySetInterVal (#7)
function mySetInterVal (fn, a, b) { let delay = 0 let shouldStop = false function start () { if (shouldStop) return const time = a + (delay++ * b) setTimeout(() => { fn() start() }, time) } start() function stop () { shouldStop = true } return stop } function myClear (cb) { cb() } // 用例 let count = 0 function fn () { console.log(++count) } const timer = mySetInterVal(fn, 1000, 500) setTimeout(() => { myClear(timer) }, 10000)
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you commented.Message ID: ***@***.***>
Activity
Genzhen commentedon Jun 22, 2020
luobolin commentedon Jul 12, 2020
zk2401 commentedon Jul 13, 2020
Freeruning commentedon Jul 13, 2020
leewr commentedon Jul 14, 2020
function mySetInterVal(fun, a, b) {
!mySetInterVal.prototype.maxLimit && (mySetInterVal.prototype.maxLimit = a + 2 * b)
if (a > mySetInterVal.prototype.maxLimit) {
mySetInterVal.prototype.maxLimit = null
return
}
return setTimeout(() => {
mySetInterVal(fun, a + b, b)
fun()
}, a);
}
function myClear(timeId) {
clearTimeout(timeId)
mySetInterVal.prototype.maxLimit = null
}
let timeId = mySetInterVal(() => {
console.log('----', timeId)
}, 1000, 1000)
ppoollaarr commentedon Jul 14, 2020
class mySetInterVal {
a: number;
b: number;
fn: (...args: any[]) => void;
handle: number;
count: number;
}
let timeObj = new mySetInterVal(
() => {
console.log(1);
},
1000,
1000
);
timeObj.start();
shenanheng commentedon Jul 14, 2020
EmotionBin commentedon Jul 15, 2020
cm-space commentedon Jul 15, 2020
function mySetInterVal(fn, a, b) {
this.a = a
this.b = b
this.time = 0
this.fn = fn
this.suspends=true
this.timer = null
}
mySetInterVal.prototype.strap = function () {
this.timer = function () {
setTimeout(() => {
this.fn()
this.time++
console.log(this.a + this.time * this.b)
if(this.suspends){
this.timer()
}
}, this.a + this.time * this.b)
}
this.timer()
}
mySetInterVal.prototype.suspend=function(){
this.suspends=false
}
let maybeCleanUpFn = new mySetInterVal(() => {
console.log('执行回调')
}, 1000, 2000)
maybeCleanUpFn.strap()
setTimeout(function () {
maybeCleanUpFn.suspend()
},100000)
Bobyoung0719 commentedon Jul 16, 2020
let timer = null;
HuberTRoy commentedon Jul 16, 2020
74 remaining items
Rednaxela-2 commentedon Jan 20, 2022
//第 1 题:写一个 mySetInterVal(fn, a, b),每次间隔 a,a+b,a+2b,...,a+nb 的时间,然后写一个 myClear,停止上面的mySetInterVal
//本鼠鼠自我感觉良好,有无大佬指教一下捏,lkd勿扰
function mySetInterVal(fn,a,b,stopMoment) {
let count = 0, timer = null;
(function wrapperFun(time) {
timer = setTimeout(()=>{
fn();
console.log(
程序运行了${count+1}轮,距离上一轮${a+count*b}毫秒
)count++;
wrapperFun(a+countb)
},time)
})(a+countb);
setTimeout(()=>{
myClear(timer)
},stopMoment)
}
function myClear(timer) {
clearTimeout(timer)
}
mySetInterVal(()=>{console.log("输出测试文字");},1000,500,5000);
kangyana commentedon Feb 15, 2022
chihaoduodongxi commentedon Feb 18, 2022
function mySetInterVal(fn,a,b){
this.handle=0
this.timer=-1;
this.start=()=>{
this.timer=setTimeout(()=>{fn();this.handle++;this.start()},a+this.handle*b);
}
this.stop=()=>{clearTimeout(this.timer);}
}
let a=new mySetInterVal(()=>{console.log('123')},1000,1000);
a.start();
a.stop();
avenir-zhang commentedon Feb 21, 2022
wmlgl commentedon Mar 7, 2022
lang711 commentedon Aug 25, 2022
huanmiezhicheng commentedon Oct 25, 2022
class MySetInterval {
constructor(fn, a, b) {
this.fn = fn
this.a = a
this.b = b
this.time = 0
this.handle = null
}
start() {
this.handle = setTimeout(() => {
this.fn()
this.time++
this.start()
}, this.a + this.time * this.b)
}
stop() {
clearTimeout(this.handle)
this.time = 0
}
}
let test = new MySetInterval(
() => {
console.log(111111)
},
1000,
2000
)
test.start()
captain-kuan commentedon Dec 14, 2022
Charles-ShiZ commentedon Feb 22, 2023
ResidualStar commentedon May 24, 2023
drlsxs commentedon Jun 20, 2023
Kisthanny commentedon Mar 20, 2024
HenuAJY commentedon Apr 10, 2024
Amorcy commentedon Sep 6, 2024
captain-kuan commentedon Sep 6, 2024