From 521d51ea5c3393313fbda2b72d46673b4b9a3b4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=94=A1=E7=8E=B2?= <8702040+cai-ling@user.noreply.gitee.com> Date: Mon, 18 Nov 2024 15:28:38 +0800 Subject: [PATCH] =?UTF-8?q?setInterval=E6=94=B9=E6=88=90setTimeout?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/home/home.vue | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/pages/home/home.vue b/pages/home/home.vue index f607216..6dc2f9d 100644 --- a/pages/home/home.vue +++ b/pages/home/home.vue @@ -51,19 +51,25 @@ }, onLoad () { this._getBillsCount() - this.intervalId = setInterval(() => { - this._getBillsCount() - }, this.interTime) + this.timerFun(this._getBillsCount, this.interTime)() }, beforeDestroy () { - clearInterval(this.intervalId) - this.intervalId = null - }, - destroyed () { - clearInterval(this.intervalId) - this.intervalId = null + if (this.intervalId !== null) { + clearTimeout(this.intervalId) + this.intervalId = null + } }, methods: { + timerFun(f, time) { + let _this = this + return function backFun() { + clearTimeout(_this.intervalId) + _this.intervalId = setTimeout(function () { + f() + backFun() + }, time) + } + }, async _getBillsCount () { let res = await getBillsCount() if (res.code === 1) {