FE-Interview
FE-Interview copied to clipboard
第 1 题:写一个 mySetInterVal(fn, a, b),每次间隔 a,a+b,a+2b,...,a+nb 的时间,然后写一个 myClear,停止上面的 mySetInterVal
欢迎在下方发表您的优质见解
function mySetInterVal(fn, a, b) {
this.a = a;
this.b = b;
this.time = 0;
this.handle = -1;
this.start = () => {
this.handle = setTimeout(() => {
fn();
this.time++;
this.start();
console.log( this.a + this.time * this.b);
}, this.a + this.time * this.b);
}
this.stop = () => {
clearTimeout(this.handle);
this.time = 0;
}
}
var a = new mySetInterVal(() => {console.log('123')},1000, 2000 );
a.start();
a.stop();
export interface MySetInterValReturn {
id: NodeJS.Timeout
}
export const mySetInterVal = (fn: (...args: any[]) => void, a: number, b: number): MySetInterValReturn => {
let timeObj: MySetInterValReturn = { id: null }
const helper = (timeout: number): void => {
timeObj.id = setTimeout(() => {
fn()
helper(timeout + b)
}, timeout);
}
helper(a)
return timeObj
}
export const myClear = (timeObj: NodeJS.Timeout): void => {
clearTimeout(timeObj)
}
// 测试用例
const timeObj = mySetInterVal((): void => {
console.log(`time: ${new Date().getSeconds()}`)
}, 1000, 1000)
setTimeout(() => myClear(timeObj.id), 5000);
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);
}
//test
const timer=mySetInterVal(()=>{console.log('run')},100,200);
setTimeout(()=>myClear(timer),2000);
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()
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)
class mySetInterVal { a: number; b: number; fn: (...args: any[]) => void; handle: number; count: number;
constructor(fn: (...args: any[]) => void, a: number, b: number) {
this.a = a;
this.b = b;
this.fn = fn;
this.count = 0;
}
start() {
this.handle = setTimeout(() => {
this.fn();
this.count++;
this.start();
console.log(this.a + this.count * this.b);
}, this.a + this.count * this.b);
}
stop() {
clearTimeout(this.handle);
this.count = 0;
}
}
let timeObj = new mySetInterVal( () => { console.log(1); }, 1000, 1000 ); timeObj.start();
function mySetInterVal(fn, a, b) {
// 停止的标识
let obj = {
timer: null,
};
let queue = [a, a + b, a + 2 * b];
return () => {
function run(arr) {
if (arr.length) {
obj.timer = setTimeout(() => {
fn();
run(arr);
}, arr.shift());
}
}
run(queue);
return obj;
};
}
function myClear(obj) {
clearTimeout(obj.timer);
}
let obj = mySetInterVal(
() => {
console.log("在哪里");
},
1000,
2000
)();
setTimeout(() => {
myClear(obj);
}, 3000);
function mySetInterVal(fn, a, b) {
let currentTimeout = null;
let counter = 0;
const step = () => {
currentTimeout = setTimeout(() => {
fn();
counter === 2 ? counter = 0 : counter ++;
step();
}, a + counter * b);
}
step();
return () => {
clearTimeout(currentTimeout);
}
}
const myClear = mySetInterVal(() => console.log(11), 1000, 2000);
// 11秒后停止
const id = setTimeout(() => {
myClear();
clearTimeout(id);
}, 11000);
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)
let timer = null;
function mySetInterVal(t1, t2) {
const T = t1 + t2;
if(t1 > 6000) {
return myClear();
}
timer = setTimeout(() => {
t2 += t1;
mySetInterVal(t1, t2);
}, T);
}
function myClear() {
clearTimeout(timer);
}
mySetInterVal(1000, 1000);
function getCurrentTime(a, b) {
let cache = -1
return function () {
cache += 1
return !cache ? a : a + cache*b
}
}
function mySetInter(fn, a, b) {
const _getCurrentTime = getCurrentTime(a, b)
let clear = {
timer: null
}
function _setTimeout() {
return setTimeout(() => {
fn();
clear.timer = _setTimeout()
}, _getCurrentTime())
}
clear.timer = _setTimeout()
return clear
}
function myClear(timer) {
clearTimeout(timer.timer)
}
let p = mySetInter(() => {console.log('hello world') }, 1000, 1000)
function mySetInterval (fn, a, b) {
let _interval = [];
let clear;
const timeout = () => {
clear = setTimeout( () => {
fn();
if (_interval.length) timeout();
}, _interval.shift() )
};
this.myClear = () => clearTimeout( clear );
this.start = () => {
_interval = [ a, a + b, a + (2 * b) ];
timeout();
}
};
const runInterval = new mySetInterval( () => console.log('run'), 1000, 2000 )
function mySetInterVal(fn, a, b) {
// 执行次数
let times = 0;
// 计时器
let timerId
function _interval() {
let duration = a + times * b;
// 清除前一次的timeout
timerId && clearTimeout(timerId)
timerId = setTimeout(function() {
fn.call(null)
times++;
_interval()
}, duration)
}
_interval();
return function clear(){
timerId && clearTimeout(timerId)
}
}
const myClear = mySetInterVal(() => console.log('hello world'), 100, 200)
setTimeout(myClear, 2000)
// 第 1 题:写一个 mySetInterVal(fn, a, b),每次间隔 a,a+b,a+2b 的时间,然后写一个 myClear,停止上面的 mySetInterVal
// export {};
class MySetInterval {
constructor(fn, a, b) {
this.a = a;
this.b = b;
this.fn = fn;
this.timer = null;
this.times = 0;
}
start() {
this.timer = setTimeout(() => {
typeof this.fn === "function" && this.fn();
this.times++;
this.start();
console.log(`this.a + this.times * this.b`, this.a + this.times * this.b);
}, this.a + this.times * this.b);
}
stop() {
if (this.timer) {
clearTimeout(this.timer);
this.timer = null;
}
this.times = 0;
}
}
const mySetInterval = new MySetInterval(
() => {
console.log("123");
},
1000,
2000
);
mySetInterval.start();
mySetInterval.stop();
题目不太清晰啊,是每次间隔以此类推吧
题目不太清晰啊,是每次间隔以此类推吧
@Marckon 是有点,改了,感谢提出问题
function mySetInterVal(fn, a, b) {
let timeOut = {
timeOutId: 0,
intervalId: 0
}
timeOut.timeOutId = setTimeout(() => {
fn()
timeOut.intervalId = setInterval(fn, b)
}, a)
return timeOut
}
function myClear(timeOut){
clearTimeout(timeOut.timeOutId)
clearInterval(timeOut.intervalId)
}
// 没有人比我的代码更短!
function mySetInterVal(fn, a, b) {
let timer= setTimeout(() => {
fn()
mySetInterVal(fn,a+b,b)
}, a)
return () => {
clearTimeout(timer)
}
}
const myClear =mySetInterVal(()=>{console.log('abc')},1000,500);
// 清除定时器
myClear()
讲真,我觉得我这个有点东西
function mySetInterVal (fn, a, b) {
let handle = {
i: 0,
stop: false
}
let realIntFn = function () {
let set
if (!handle.stop) {
set = setTimeout(() => {
console.log(`a + ${handle.i}b`, a + b * handle.i)
fn()
handle.i++
realIntFn()
}, a + b * handle.i)
} else {
clearTimeout(set)
}
}
realIntFn()
return {
stop: function () {
handle.stop = true
}
}
}
function myClear (intVal) {
intVal.stop()
}
module.exports = {
mySetInterVal,
myClear
}
// 测试 test.js
const { mySetInterVal, myClear } = require('./mySetIntVal.js')
const s = mySetInterVal(() => {
console.log(s)
}, 1000, 1000)
setTimeout(() => {
myClear(s)
}, 6000)
// code by [email protected]
function mySetInterVal(fn, a, b) {
this.a = a;
this.b = b;
this.time = 0;
this.handle = null;
this.start = () => {
this.handle = setTimeout(() => {
fn()
this.time++;
this.start();
console.log('执行调用中:', this.a + this.time * this.b)
}, this.a + this.time * this.b)
}
this.start()
this.stop = () => {
clearTimeout(this.handle)
this.time = 0;
console.log('执行已被清理,结束执行')
}
return this.stop
}
let myClear = mySetInterVal(function() {console.log('间隔执行了')}, 1000, 1000);
setTimeout(myClear, 10000)
function mySetInterVal(fn, a, b) {
let step = 0
let timer = null
const create = (timeout) => {
timer = setTimeout(() => {
fn(a + step * b)
step += 1
create(a + step * b)
}, timeout);
}
create(a + step * b)
return {
myClear() {
console.log("结束了")
clearTimeout(timer)
}
}
}
const {
myClear
} = mySetInterVal((val) => console.log(val), 1000, 1000)
setTimeout(() => {
myClear()
}, 10000)
let i = 0;
function log() {
console.log(i++);
}
function wait(time = 300) {
return new Promise((resolve) => {
setTimeout(() => {
resolve();
}, time);
});
}
function mySetInterVal(fn, a, b) {
this.switchBol = true;
this.limitTime = a;
const vm = this;
(async () => {
while(vm.switchBol) {
await wait(vm.limitTime);
fn();
vm.limitTime += b;
}
})();
return vm;
}
function myClear(timer) {
timer.switchBol = false;
}
const timer = mySetInterVal(log, 1000, 1000);
// 测试
setTimeout(() => {
myClear(timer);
}, 5000);
function mySetInterVal(fn, a, b) {
const maxLen = 3;
let index = 0;
function callback() {
if (index >= 0) {
return;
}
const current = a + b * (index % maxLen);
setTimeout(() => {
index++;
if (index >= 0) {
fn(current);
callback();
}
}, current);
}
callback();
return function() {
index = -2;
};
};
const setInterValStop = mySetInterVal((time) => {
console.log(time);
}, 1000, 2000);
function myClear() {
setInterValStop();
}
借鉴前面的思路,通过对象传递的是引用的特性,获取timeout id
function mySetInterVal(fn, a, b) {
const timer = {};
function circle(duration) {
myClear(timer);
timer.id = setTimeout(() => {
fn();
circle(duration + b);
}, duration);
return timer;
}
return circle(a);
}
function myClear(timer) {
if (timer.id) {
clearTimeout(timer.id);
}
}
const foo = () => {
console.log("a");
};
const timer = mySetInterVal(foo, 1000, 1000);
setTimeout(() => {
myClear(timer);
}, 5000);
function mySetInterVal(fn, a, b) {
let timer;
function timeout(fn, i) {
timer = setTimeout(() => {
fn();
timeout(fn, i + 1);
}, a + i * b);
}
timeout(fn, 0);
function getTimer() {
return timer;
}
return getTimer;
}
let getTimer = mySetInterVal(() => {
console.log(Math.ceil(Date.now() / 1000));
}, 1000, 1000);
function myCelar(timer) {
clearTimeout(timer);
}
setTimeout(() => {
myCelar(getTimer())
}, 10000);
function mySetInterval(fn, a, b) { let timeId = {}; (function timeCircle(timeId, fn, a, b) { timeId.id = setTimeout(() => { fn(); timeCircle(timeId, fn, a+b, b); }, a); })(timeId, fn, a, b); return timeId }
function myClear(timeId){ clearTimeout(timeId); }
//test let timeId = mySetInterval(function(){ console.log(Date.now()); }, 1000, 1000); setTimeout(() => { myClear(timeId.id); }, 10000);
function mySetInterVal(fn, a, b) {
let interval = null
let count = 0;
function delayFn() {
interval = setTimeout(function() {
fn();
count++;
delayFn();
}, a + count * b);
}
delayFn()
return function() {
clearInterval(interval);
};
}
function mySetInterVal(fn, a, b){
let count = 0;
let timer;
(function loop(){
timer = setTimeout(()=> {
fn()
count ++;
loop()
}, a + b * count)
})()
return function(){
clearTimeout(timer)
}
}
let myClear = mySetInterVal(() => console.log('hello'), 1000, 1000)
// 没有人比我的代码更短! function mySetInterVal(fn, a, b) { let timer= setTimeout(() => { fn() mySetInterVal(fn,a+b,b) }, a) return () => { clearTimeout(timer) } } const myClear =mySetInterVal(()=>{console.log('abc')},1000,500); // 清除定时器 myClear()
有问题 return 的timer是第一次的timer,改一下
function mySetInterVal(fn, a, b) {
let timer = null;
(function test(fn,a,b){
timer = setTimeout(() => {
fn()
test(fn,a+b,b)
}, a);
})(...arguments)
return () => {
clearTimeout(timer)
}
}
var myClear =mySetInterVal(()=>{console.log('abc')},1000,500);
var mySetInterval = (fn, a, b) => {
let i = 0;
let obj = {
timer: -1,
};
const clear = () => {
clearTimeout(obj.timer);
};
const start = () => {
obj.timer = setTimeout(() => {
fn();
clear();
start();
}, a + i++ * b);
};
return {
start,
clear,
};
};
// test
const { start, clear } = mySetInterval(() => console.log("test"), 2e3, 2e3);
start();
clear();