diff --git a/app.js b/app.js index 133b06c..2ed92c9 100644 --- a/app.js +++ b/app.js @@ -351,5 +351,11 @@ App({ clearTimeout(timer); onResult(res); }); + // 返回取消函数,供组件/页面在销毁时调用,静默取消超时 + return () => { + if (settled) return; + settled = true; + clearTimeout(timer); + }; }, }) \ No newline at end of file diff --git a/components/configureDevice_2/configureDevice_2.js b/components/configureDevice_2/configureDevice_2.js index 43082f6..1e8c570 100644 --- a/components/configureDevice_2/configureDevice_2.js +++ b/components/configureDevice_2/configureDevice_2.js @@ -53,10 +53,10 @@ Component({ this.fetchUserList(); }, detached() { - // 4. 在组件销毁时取消监听,防止内存泄漏 if (this._connectStateWatcher) { app.unwatch('ppScale.device.connectState', this._connectStateWatcher); } + if (this._sdkCancel) { this._sdkCancel(); this._sdkCancel = null; } } }, @@ -190,7 +190,7 @@ Component({ // 获取设备端已有用户,与用户列表做双向同步 wx.showLoading({ title: "正在同步用户信息...", mask: true }); - app.callWithTimeout( + this._sdkCancel = app.callWithTimeout( (done) => app.globalData.ppScale.activeProtocol.dataFetchUserID(done), SDK_TIMEOUT, (deviceUserIds) => { @@ -243,7 +243,7 @@ Component({ } wx.showLoading({ title: "正在下发第" + (syncIndex + 1) + "/" + needSyncList.length + "个用户...", mask: true }); let user = needSyncList[syncIndex]; - app.callWithTimeout( + this._sdkCancel = app.callWithTimeout( (done) => app.globalData.ppScale.activeProtocol.dataSyncUserInfo({ userID: user.userId, userName: user.realname, @@ -290,7 +290,7 @@ Component({ } wx.showLoading({ title: "正在删除第" + (delIndex + 1) + "/" + needDeleteList.length + "个多余用户...", mask: true }); let item = needDeleteList[delIndex]; - app.callWithTimeout( + this._sdkCancel = app.callWithTimeout( (done) => app.globalData.ppScale.activeProtocol.dataDeleteUser({ userID: item.userID, memberID: "" @@ -330,7 +330,7 @@ Component({ */ _checkWifiAndNext() { wx.showLoading({ title: "正在检测配网状态...", mask: true }); - app.callWithTimeout( + this._sdkCancel = app.callWithTimeout( (done) => app.globalData.ppScale.activeProtocol.codeFetchWifiConfig(done), SDK_TIMEOUT, (res) => { diff --git a/components/configureDevice_3/configureDevice_3.js b/components/configureDevice_3/configureDevice_3.js index 817a785..54a0172 100644 --- a/components/configureDevice_3/configureDevice_3.js +++ b/components/configureDevice_3/configureDevice_3.js @@ -20,6 +20,7 @@ Component({ }, detached() { console.log('configureDevice3 detached'); + if (this._sdkCancel) { this._sdkCancel(); this._sdkCancel = null; } } }, @@ -34,7 +35,7 @@ Component({ wx.showToast({ title: '设备未连接,请重试', icon: 'none' }); return; } - app.callWithTimeout( + this._sdkCancel = app.callWithTimeout( (done) => activeProtocol.dataFindSurroundDevice(done), SDK_TIMEOUT, (res) => { @@ -83,7 +84,7 @@ Component({ let version = app.globalData.ppScale.version; if (version === 'domain1') { - app.callWithTimeout( + this._sdkCancel = app.callWithTimeout( (done) => app.globalData.ppScale.activeProtocol.dataConfigNetWork({ domain: app.globalData.ppScale.domain1, ssid: this.data.selectWifi.ssid, @@ -101,7 +102,7 @@ Component({ ); } if (version === 'domain2') { - app.callWithTimeout( + this._sdkCancel = app.callWithTimeout( (done) => app.globalData.ppScale.activeProtocol.dataConfigUserNetWork({ domain: app.globalData.ppScale.domain2, ssid: this.data.selectWifi.ssid, diff --git a/components/replaceNetwork_3/replaceNetwork_3.js b/components/replaceNetwork_3/replaceNetwork_3.js index 0a1c60c..c57571a 100644 --- a/components/replaceNetwork_3/replaceNetwork_3.js +++ b/components/replaceNetwork_3/replaceNetwork_3.js @@ -26,7 +26,7 @@ Component({ this.setNerwork(); }, detached() { - + if (this._sdkCancel) { this._sdkCancel(); this._sdkCancel = null; } } }, @@ -37,7 +37,7 @@ Component({ let password = this.properties.password; let version = app.globalData.ppScale.version; if (version === 'domain1') { - app.callWithTimeout( + this._sdkCancel = app.callWithTimeout( (done) => app.globalData.ppScale.activeProtocol.dataConfigNetWork({ domain: app.globalData.ppScale.domain1, ssid: ssid, @@ -57,7 +57,7 @@ Component({ ); } if (version === 'domain2') { - app.callWithTimeout( + this._sdkCancel = app.callWithTimeout( (done) => app.globalData.ppScale.activeProtocol.dataConfigUserNetWork({ domain: app.globalData.ppScale.domain2, ssid: ssid,