
首先:
一、在util.js中放卜运入如下两组函数
1. 设置点击后多久不能再次 *** 作该
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
}
}
}
2. 设置加载动画
function showLoading(message) {
if (wx.showLoading) { // 基础库 1.1.0 微信6.5.6版本开始支持,低版本需做兼容处理
wx.showLoading({
title: message, mask: true
})
} else { // 低版本型御梁采用Toast兼容处理并将时间设为20秒以免自动消失
wx.showToast({
title: message, icon: 'loading', mask: true, duration: 20000
})
}
}
function hideLoading() {
if (wx.hideLoading) { // 基础库 1.1.0 微信6.5.6版本开始支持,低版本需做兼容处理
wx.hideLoading()
} else {
wx.hideToast()
}
}
并且将其导出作为页面使用:
module.exports = {
throttle: throttle,
showLoading: showLoading,
hideLoading: hideLoading,
}
二、将函数引入页面使用
const util = require('../../utils/util.js')
即可。
一般镇庆想实现后退刷新效果,比如判断是否登录啊, 这个时候可以在 onShow 函数里面写判断是否登录,如果没有登录跳转到登录页。
再比如判断页面是否有加载某个数据, 就在onShow里面判断:如果等于空的话, 就再加载一次。
示例代码:
App({onLaunch: function(options) {
// 生命周期函数--监听小程序初始化 当小程序派旅缺初始化完成时,会触发 onLaunch(全局只触发一次)
},
onShow: function(options) {
// 生命周期函数--监听尘辩小程序显示(后退到这个页面的时候这个就会被回调) 当小程序启动,或从后台进入前台显示,会触发 onShow
},
onHide: function() {
// 生命周期函数--监听小程序隐藏 当小程序从前台进入后台,会触发 onHide
},
onError: function(msg) {
// 错误监听函数 当小程序发生脚本错误,或者 api 调用失败时,会触发 onError 并带上错误信息
},
globalData: 'I am global data'})
更详细的资料应该参照官网API看, 因为随着升级有些方法可能不再适用!
小程序API 网页链接
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)