-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
819 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--- | ||
title: ECMAScript 特性 | ||
title: ECMAScript 版本特性 | ||
--- | ||
|
||
# ECMAScript 特性 | ||
# ECMAScript 版本特性 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
--- | ||
title: 手写 JavaScript 实现防抖函数 debounce | ||
--- | ||
|
||
# 手写 JavaScript 实现防抖函数 debounce | ||
|
||
代码: | ||
|
||
```js | ||
/** | ||
* 防抖函数 | ||
* | ||
* @param {Function} fn 要防抖的函数 | ||
* @param {number} timeout 间隔时间 | ||
*/ | ||
function debounce(fn, timeout = 200) { | ||
let timer = null | ||
return function (...args) { | ||
if (timer) | ||
clearTimeout(timer) | ||
|
||
timer = setTimeout(() => { | ||
fn.apply(this, args) | ||
}, timeout) | ||
} | ||
} | ||
``` | ||
|
||
使用: | ||
|
||
```js | ||
function fn(msg) { | ||
console.log(msg) | ||
} | ||
|
||
const debounceFn = debounce(fn, 500) | ||
|
||
debounceFn('1') | ||
|
||
setTimeout(() => { | ||
debounceFn('100') | ||
}, 100) | ||
|
||
setTimeout(() => { | ||
debounceFn('200') | ||
}, 200) | ||
|
||
setTimeout(() => { | ||
debounceFn('300') | ||
}, 300) | ||
|
||
setTimeout(() => { | ||
debounceFn('400') | ||
}, 400) | ||
|
||
setTimeout(() => { | ||
debounceFn('500') | ||
}, 500) | ||
|
||
setTimeout(() => { | ||
debounceFn('600') | ||
}, 600) | ||
``` | ||
|
||
可以看到输出的是 `600`,因为在 `500ms` 内,`debounceFn` 被调用了多次,但是只有最后一次生效了。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
--- | ||
title: 手写 JavaScript 实现节流函数 throttle | ||
--- | ||
|
||
# 手写 JavaScript 实现节流函数 throttle | ||
|
||
代码: | ||
|
||
```js | ||
/** | ||
* 节流函数 | ||
* | ||
* @param {Function} fn 要节流的函数 | ||
* @param {number} timeout 间隔时间 | ||
*/ | ||
function throttle(fn, timeout) { | ||
let timer = null | ||
return function (...args) { | ||
if (timer) | ||
return | ||
|
||
timer = setTimeout(() => { | ||
timer = null | ||
fn.apply(this, args) | ||
}, timeout) | ||
} | ||
} | ||
``` | ||
|
||
使用: | ||
|
||
```js | ||
function fn(msg) { | ||
console.log(msg) | ||
} | ||
|
||
const throttleFn = throttle(fn, 500) | ||
|
||
throttleFn('1') | ||
|
||
setTimeout(() => { | ||
throttleFn('100') | ||
}, 100) | ||
|
||
setTimeout(() => { | ||
throttleFn('200') | ||
}, 200) | ||
|
||
setTimeout(() => { | ||
throttleFn('300') | ||
}, 300) | ||
|
||
setTimeout(() => { | ||
throttleFn('400') | ||
}, 400) | ||
|
||
setTimeout(() => { | ||
throttleFn('500') | ||
}, 500) | ||
|
||
setTimeout(() => { | ||
throttleFn('600') | ||
}, 600) | ||
|
||
setTimeout(() => { | ||
throttleFn('700') | ||
}, 700) | ||
``` | ||
|
||
可以看到,节流函数的执行结果如下: | ||
|
||
```text | ||
1 | ||
500 | ||
``` | ||
|
||
因为 500 毫秒内只有第一调用生效。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.