微信小程序component组件内如何实现onreachbottom触底事件回调?
page里的onreachbottom事件可以使用,但是component里面的呢?
网友回复
wxml
<scroll-view bindscrolltolower="lower" scroll-y style="height:100vh;"> </scroll-view>
记得将app.wxss的page高度设为100vh
page{ height:100vh }js
lower() { that.getData(); //加载更多数据 },
为了防止拖动下来后疯狂的加载数据,我们可以增加一个函数节流
在utils目录中新建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 } } } module.exports = { throttle: throttle }改造一下js
const util = require('../../utils/util.js') Page({ onLoad: function (options) { }, lower() { var that=this; util.throttle(function (e) { that.getData(); //加载更多数据 }, 2000) }, })
这样就控制在2秒内只能执行一次getdata函数