生命的意义在于折腾

函数节流

javascript

1/*
2 * Auth: WANGJIAN
3 */
4const throttle=(fn, gapTime) =>{
5  if (gapTime == null || gapTime == undefined) {
6    gapTime = 1500
7  }
8
9  let _lastTime = null
10
11  // 返回新的函数
12  return function () {
13    let _nowTime = + new Date()
14    if (_nowTime - _lastTime > gapTime || !_lastTime) {
15      fn.apply(this, arguments)   //将this和参数传给原函数
16      _lastTime = _nowTime
17    }
18  }
19}
20module.exports = throttle
阅读量:1526发布日期:2021-06-11 16:46:44

博客描述

常见应用场景: 比如说input输入值,值变化,请求后端数据

留言板