Skip to content

Commit

Permalink
docs: update first exec of debounce fn
Browse files Browse the repository at this point in the history
  • Loading branch information
dobble11 committed Apr 9, 2024
1 parent ebfef48 commit 453be1d
Showing 1 changed file with 5 additions and 10 deletions.
15 changes: 5 additions & 10 deletions docs/zjw/前端常见手写代码.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,18 @@ window.addEventListener('scroll', debounceTask);
```js
function debounce(fn, delay) {
var timer = null;
var first = true;

return function () {
clearTimeout(timer);

const f = fn.bind(this, ...arguments);
const callNow = !timer;

if (callNow) {
if (first) {
f();
first = false;
} else {
timer = setTimeout(f, delay);
}
timer = setTimeout(() => {
timer = null;
// 非首次调用
if (!callNow) {
f();
}
}, delay);
};
}
```
Expand Down

0 comments on commit 453be1d

Please sign in to comment.