当前位置:网站首页>微信小程序setInterval定时函数使用详细教程
微信小程序setInterval定时函数使用详细教程
2022-04-23 14:02:00 【1029179954】
1、setInterval的理解
(1)setInterval理解
setInterval定时函数,就是延迟多长时间不停的调用 setInterval中的函数,想具体了解 setInterval函数,我们先看一下 setInterval函数组成部分。
(2)setInterval组成
setInterval(function () {},时间)。function () {}就是不停执行函数,时间就是延迟多久不断地执行,重点function () {}函数
具体示例:
setInterval(function () {
//.toClock1()是具体函数,写在外边
_this.toClock1();}, 6000);
(3)setInterval需要关闭
使用clearInterval()去关闭,具体使用看下面的内容
clearInterval()
2、setInterval放在微信小程序onshow函数里
onShow:页面显示或从后台跳回小程序时显示此页面时触发,从跳转页面返回时触发,不能传递参数
3、setInterval具体使用
(1)设置全局变量timer(timer随便起)
//在微信小程序data中写如下代码,timer全局变量
data: {
timer: null,
},
(2)onshow写setInterval函数
onShow: function () {
const _this = this
//定时器 函数赋值给timer 方便clearInterval()使用
_this.data.timer = setInterval(
function () {
_this.toClock1();
}, 6000);
_this.setData({
timer:_this.data.timer
});
},
toClock1()函数
//定时函数执行的内容 自己发挥 写自己的代码
toClock1(){
console.log(this.data.timer)
}
3、离开当前页面关闭 setInterval定时函数
代码放在onhide里边
onHide: function () {
//关闭clearInterval定时函数
clearInterval(this.data.timer);
this.setData({
timer: null
});
console.log(this.data.timer)
},
微信小程序更多实用技巧关注下面公众号
版权声明
本文为[1029179954]所创,转载请带上原文链接,感谢
https://blog.csdn.net/baidu_38978508/article/details/123289651
边栏推荐
猜你喜欢
浅谈基于openssl的多级证书,Multi-level CA的签发和管理,以及双向认证
Nodejs安装及环境配置
Decentralized Collaborative Learning Framework for Next POI Recommendation
Lin Lin, product manager of Lenovo: network failure of local network operator in Tianjin. The background server of Zui system can't work normally for the time being
程序编译调试学习记录
解决方案架构师的小锦囊 - 架构图的 5 种类型
Quartus prime hardware experimental development (de2-115 board) experiment II function adjustable comprehensive timer design
Android 面试主题集合整理
PATH环境变量
Qt Designer怎样加入资源文件
随机推荐
编程旅行之函数
Oracle告警日志alert.log和跟踪trace文件中文乱码显示
接口文档yaml
解决方案架构师的小锦囊 - 架构图的 5 种类型
Universal template for scikit learn model construction
生成随机高质量符合高斯分布的随机数
变长参数__VA_ARGS__ 和 写日志的宏定义
Choreographer全解析
商家案例 | 运动健康APP用户促活怎么做?做好这几点足矣
Jenkins construction and use
mysql新表,自增id长达20位,原因竟是......
leetcode--380.O(1) 时间插入、删除和获取随机元素
趣谈网络协议
收藏博客贴
cnpm的诡异bug
JS brain burning interview question reward
China creates vast research infrastructure to support ambitious climate goals
Lin Lin, product manager of Lenovo: network failure of local network operator in Tianjin. The background server of Zui system can't work normally for the time being
2022年江西最新建筑八大员(质量员)模拟考试题库及答案解析
Express②(路由)