util.js 415 B

1234567891011121314151617181920
  1. function throttle(fn, gapTime) {
  2. if (gapTime == null || gapTime == undefined) {
  3. gapTime = 1500
  4. }
  5. let _lastTime = null
  6. // 返回新的函数
  7. return function () {
  8. let _nowTime = + new Date()
  9. if (_nowTime - _lastTime > gapTime || !_lastTime) {
  10. fn.apply(this, arguments) //将this和参数传给原函数
  11. _lastTime = _nowTime
  12. }
  13. }
  14. }
  15. module.exports = {
  16. throttle: throttle
  17. }