当前位置:网站首页>Detailed tutorial on the use of setinterval timing function of wechat applet

Detailed tutorial on the use of setinterval timing function of wechat applet

2022-04-23 14:05:00 one billion twenty-nine million one hundred and seventy-nine th

1、setInterval The understanding of the
(1)setInterval understand
setInterval Timing function , Is how long to delay the non-stop call setInterval The function in , I want to know more about setInterval function , Let's take a look first setInterval Function components .
(2)setInterval form
setInterval(function () {}, Time ).function () {} Just keep executing functions , Time is how long it's delayed. Keep executing , a key function () {} function
Concrete example :

 setInterval(function () {
 				//.toClock1() Is a concrete function , Write outside 
 				_this.toClock1();}, 6000);

(3)setInterval Need to be closed
Use clearInterval() To close , See the following for specific use

clearInterval()

2、setInterval Put it in wechat applet onshow In the function
onShow: Triggered when this page is displayed or when you jump back to the applet from the background , Triggered when returning from the jump page , Can't pass parameters
3、setInterval Specific use
(1) Set global variables timer(timer Whatever the )

// In wechat applet data Write the following code in ,timer Global variables 
 data: {
    timer: null,
  },

(2)onshow Write setInterval function

onShow: function () {
      const _this = this
       // Timer    The function is assigned to timer   convenient clearInterval() Use 
       _this.data.timer = setInterval(
         function () {
        _this.toClock1();        
        }, 6000);

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

toClock1() function

// Contents of timing function execution    Play by yourself     Write your own code 
toClock1(){
	console.log(this.data.timer)
}

3、 Leave the current page and close setInterval Timing function
Code placed in onhide inside

onHide: function () {
// close clearInterval Timing function 
    clearInterval(this.data.timer);
    this.setData({
      timer: null
    });
    console.log(this.data.timer)
  },

WeChat applet more practical skills, pay attention to the official account below.
 Insert picture description here

版权声明
本文为[one billion twenty-nine million one hundred and seventy-nine th]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231401297853.html