欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

微信小程序setInterval定時(shí)函數(shù)新手使用的超詳細(xì)教程

 更新時(shí)間:2022年08月03日 09:19:00   作者:1029179954  
平時(shí)開(kāi)發(fā)中為實(shí)現(xiàn)倒計(jì)時(shí)效果可以使用setInterval即可,下面這篇文章主要給大家介紹了關(guān)于微信小程序setInterval定時(shí)函數(shù)新手使用的超詳細(xì)教程,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下

1、setInterval的理解

(1)setInterval理解

setInterval定時(shí)函數(shù),就是延遲多長(zhǎng)時(shí)間不停的調(diào)用 setInterval中的函數(shù),想具體了解 setInterval函數(shù),我們先看一下 setInterval函數(shù)組成部分。

(2)setInterval組成

setInterval(function () {},時(shí)間)。function () {}就是不停執(zhí)行函數(shù),時(shí)間就是延遲多久不斷地執(zhí)行,重點(diǎn)function () {}函數(shù)

具體示例:

 setInterval(function () {
                 //.toClock1()是具體函數(shù),寫(xiě)在外邊
                 _this.toClock1();}, 6000);

(3)setInterval需要關(guān)閉

使用clearInterval()去關(guān)閉,具體使用看下面的內(nèi)容

clearInterval()

2、setInterval放在微信小程序onshow函數(shù)里

onShow:頁(yè)面顯示或從后臺(tái)跳回小程序時(shí)顯示此頁(yè)面時(shí)觸發(fā),從跳轉(zhuǎn)頁(yè)面返回時(shí)觸發(fā),不能傳遞參數(shù)

3、setInterval具體使用

(1)設(shè)置全局變量timer(timer隨便起)

//在微信小程序data中寫(xiě)如下代碼,timer全局變量
 data: {
    timer: null,
  },

(2)onshow寫(xiě)setInterval函數(shù)

onShow: function () {
? ? ? const _this = this
? ? ? ?//定時(shí)器 ?函數(shù)賦值給timer ?方便clearInterval()使用
? ? ? ?_this.data.timer = setInterval(
? ? ? ? ?function () {
? ? ? ? _this.toClock1(); ? ? ? ?
? ? ? ? }, 6000);

?? ? ? ?_this.setData({
?? ? ? ? ?timer:_this.data.timer
?? ? ? ?});
? },

toClock1()函數(shù)

//定時(shí)函數(shù)執(zhí)行的內(nèi)容  自己發(fā)揮   寫(xiě)自己的代碼
toClock1(){
    console.log(this.data.timer)
}

4、離開(kāi)當(dāng)前頁(yè)面關(guān)閉 setInterval定時(shí)函數(shù)

代碼放在onhide里邊

onHide: function () {
//關(guān)閉clearInterval定時(shí)函數(shù)
    clearInterval(this.data.timer);
    this.setData({
      timer: null
    });
    console.log(this.data.timer)
  },

附:微信小程序定時(shí)器setInterval()的使用注意事項(xiàng)

setInterval(function(){}, number 時(shí)間間隔/ms)

注意在setInterval中定義的函數(shù)中使用  this  指向的是該計(jì)時(shí)器,若要用到頁(yè)面數(shù)據(jù)應(yīng)如下操作:

let that=this
setInterval(function(){
? ?that.data.a=0;
},number 時(shí)間間隔/ms)

通過(guò)在setInterval外面設(shè)置一個(gè)變量 that 獲得 頁(yè)面 this 的引用,后進(jìn)行操作

總結(jié)

到此這篇關(guān)于微信小程序setInterval定時(shí)函數(shù)使用的文章就介紹到這了,更多相關(guān)微信小程序setInterval定時(shí)函數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論