uniapp小程序,实现每次进入页面时自动开启 30 秒的倒计时。

<view class="number">{{count}}</view> 
<!-- 倒计时数字 -->
data() {
    return {
      count: 30,
      timer: null,
    };
},
methods: {
	  countDown() {
      	let _this = this;
      	const TIME_COUNT = 30;
      	if (!this.timer) {
        	this.count = TIME_COUNT;
       		this.timer = setInterval(() => {
        if (this.count > 0 && this.count <= TIME_COUNT) {
            this.count--;
        } else {
            clearInterval(this.timer);
            this.timer = null;
          }
        }, 1000);
      }
    },
},
onShow() {
    this.countDown();
}

  

posted @ 2022-04-08 20:35  夏冬青  阅读(680)  评论(0编辑  收藏  举报