Skip to content

Commit

Permalink
实现:一段时间内方法1只执行1遍,若再执行则执行方法2
Browse files Browse the repository at this point in the history
  • Loading branch information
realgeoffrey committed Aug 28, 2024
1 parent e0e33d4 commit 37cdb5f
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions 网站前端/JS方法积累/手写代码/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,28 @@ console.log(deepClone(obj));
};
}
```
><details>
><summary>实现:一段时间内方法1只执行1遍,若再执行则执行方法2</summary>
>
>```js
>function execute2(fn1, fn2, timeout) {
> let flag = false;
>
> return function (...args) {
> if (!flag) {
> fn1.apply(this, args); // 第一次执行的函数
> flag = true;
> setTimeout(() => {
> flag = false;
> }, timeout);
> } else {
> fn2.apply(this, args); // 后续执行的函数
> }
> };
>}
>```
></details>
2. 节流函数
1. 解法一
Expand Down

0 comments on commit 37cdb5f

Please sign in to comment.