直接贴代码吧
在util.js
中,封装函数,然后在其他地方进行使用
//util.js
function throttle(fn, gapTime) {
if (gapTime == null || gapTime == undefined) {
gapTime = 1500
}
let _lastTime = null
// 返回新的函数
return function () {
let _nowTime = + new Date()
if (_nowTime - _lastTime > gapTime || !_lastTime) {
fn.apply(this, arguments) //将this和参数传给原函数
_lastTime = _nowTime
}
}
}
使用
// index.wxml
<view bind:tap="handleClick">提交表单</view>
// index.js
const util = require('../../../util')
// 点击提交表单
handleClick: util.throttle(function, (e) {
// 书写业务逻辑
},1000)