javascript
Page({
onUnload {
if (this.timer) clearInterval(this.timer);
this.data.eventListener && this.data.eventListener.remove;
});
javascript
onUnload {
this.audioCtx && this.audioCtx.destroy;
javascript
// 使用后释放
const app = getApp;
app.largeData = null;
javascript
// 在页面中监听滚动,动态加载图片
Page({
data: { inViewport: [] },
onReady {
this.query = wx.createSelectorQuery;
// 检查图片是否进入视口
this.checkImages;
},
checkImages {
this.query.selectAll('.lazy-img').boundingClientRect(rects => {
rects.forEach((rect, index) => {
if (rect.top <= wx.getSystemInfoSync.windowHeight) {
// 触发图片加载
const key = `inViewport[${index}]`;
this.setData({ [key]: true });
});
}).exec;
// 滚动时再次检查
wx.onWindowScroll( => this.checkImages);
},
onUnload {
wx.offWindowScroll; // 移除滚动监听
});
通过以上步骤,可有效减少内存占用,避免小程序因内存不足导致的崩溃或卡顿。
版权声明: 知妳网保留所有权利,部分内容为网络收集,如有侵权,请联系QQ793061840删除,添加请注明来意。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态
