在/utils/util.js(工具類)加入以下代碼
module.exports = {
throttle: throttle
}
// 函數節流(throttle):函數在一段時間內多次觸發只會執行第一次
const throttle = (fn, gapTime) => {
if (gapTime == null || gapTime == undefined) {
gapTime = 1500
}
let _lastTime = null
return function () {
let _nowTime = + new Date()
if (_nowTime - _lastTime > gapTime || !_lastTime) {
// 將this和參數傳給原函數
fn.apply(this, arguments)
_lastTime = _nowTime
}
}
}
在使用的頁面:在/index/index.js使用
toStoreList是click的事件名
toStoreList: util.throttle(function () {
console.log('111111111')
},1000),