Skip to content

Commit df827b0

Browse files
committed
feat: update debounce & throttle
1 parent 67dc68d commit df827b0

File tree

4 files changed

+32
-33
lines changed

4 files changed

+32
-33
lines changed

front-end-interview/debounce.js

-13
This file was deleted.

front-end-interview/debounce.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* 防抖
3+
* @param fn
4+
* @param delay
5+
*/
6+
const de = function (fn: Function, delay: number = 100) {
7+
let timer: number;
8+
return (...args: []) => {
9+
clearTimeout(timer);
10+
timer = setTimeout(() => {
11+
fn.apply(this, args);
12+
}, delay)
13+
}
14+
}

front-end-interview/throttle.js

-20
This file was deleted.

front-end-interview/throttle.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* 防抖
3+
* @param fn
4+
* @param delay
5+
*/
6+
const th = function (fn: Function, delay: number = 100) {
7+
let flag = true;
8+
return (...args: []) => {
9+
if (!flag) {
10+
return;
11+
}
12+
flag = false;
13+
setTimeout(() => {
14+
fn.apply(this, args);
15+
flag = true;
16+
}, delay)
17+
}
18+
}

0 commit comments

Comments
 (0)