gpt4 book ai didi

angular - 防止定时器在后台模式下暂停

转载 作者:搜寻专家 更新时间:2023-10-30 21:28:41 26 4
gpt4 key购买 nike

我的 Ionic3 应用程序中有一个计时器。定时器与 setInterval 完美运行,但是当我关闭应用程序进入休眠模式时,定时器停止运行。将我的应用程序再次带到 fourground 后,计时器继续 - 但他从之前暂停的地方开始。

如果我将应用设置为后台,如何防止计时器暂停?

我的组件

time: any;

displayTime() {
this.time = moment().hour(0).minute(0).second(this.counter++).format('HH : mm : ss');
}

startTime() {
if(this.runClock == null) {
this.runClock = setInterval(() => {
this.displayTime();
},1000)
}
}

在我的 HTML 中,我调用了 {{ time }}

类似https://ionicframework.com/docs/native/background-mode/ 的插件将不起作用,因为 App Store 将拒绝使用此插件的应用程序。

还有其他想法吗?

最佳答案

由于在后台模式下运行的应用程序可以是 rejected in App Store (这是有充分理由的,因为没有人需要一个会白白耗尽电池电量的应用程序),这应该在不依赖 setInterval 计数器和后台模式的情况下完成:

startMoment = moment();

displayTime() {
this.time = moment.utc( moment().diff(this.startMoment) ).format('HH : mm : ss');
}

关于angular - 防止定时器在后台模式下暂停,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47249088/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com