[英]setInterval/setTimer not running after 5 mins of background time - Ionic
I have a requirement of a timer, that should send out notification (I am used onesignal API notification) after a particular user set value is reached.我需要一个计时器,它应该在达到特定用户设置值后发出通知(我使用的是一个信号 API 通知)。
For example if a user sets a value of 7 mins, the timer should send notification after 7 mins.例如,如果用户将值设置为 7 分钟,则计时器应在 7 分钟后发送通知。 The time a user can set varies from 1-59 mins.用户可以设置的时间范围为 1-59 分钟。 But even with using the background mode plugin ( https://github.com/katzer/cordova-plugin-background-mode ), I cannot get the setInterval/setTimeout functions to work after 5 mins.但即使使用后台模式插件( https://github.com/katzer/cordova-plugin-background-mode ),我也无法让 setInterval/setTimeout 函数在 5 分钟后工作。
Tried using recursive setTimeout method:尝试使用递归 setTimeout 方法:
function startTimerCounter() {
this.timerCounter = 0;
const objThis = this; //store this.
if (this.timerCounter >= this.setTime) {
this.backgroundMode.disable();
this.goalTimerReached = true;
} else {
this.backgroundMode.enable();
this.timer = setTimeout(function request() {
if (objThis.timerCounter === objThis.setTime) {
//onesignal notification
clearTimeout(objThis.timer);
} else {
++objThis.timerCounter;
objThis.timer = setTimeout(request, 1000);
}
}, 1000);
}
}
Tried using the setInterval method:尝试使用 setInterval 方法:
function startTimerCounter() {
this.timerCounter = 0;
const objThis = this; //store this.
if (this.timerCounter >= this.setTime) {
this.backgroundMode.disable();
this.goalTimerReached = true;
} else {
this.backgroundMode.enable();
this.timerCounter = 0;
const objThis = this; //store this.
this.timer = setInterval(() => {
if (objThis.timerCounter === objThis.setTime) {
//onesignal notification
clearInterval(objThis.timer);
} else {
++objThis.timerCounter;
}
}, 1000);
}
}
The background activation notification at the top and I can see that the background mode is active.顶部的后台激活通知,我可以看到后台模式处于活动状态。 But the timer doesn't seem to be running after 5 mins.但是计时器似乎在 5 分钟后没有运行。
any idea how this could be solved?知道如何解决吗?
*** Update **** *** 更新 ****
Tried to invoke the function every 4 mins in the background to keep running which didn't work for me:试图在后台每 4 分钟调用一次 function 以继续运行,但这对我不起作用:
function startTimerCounter() {
this.timerCounter = 0;
const objThis = this; //store this.
if (this.timerCounter >= this.setTime) {
this.backgroundMode.disable();
this.goalTimerReached = true;
} else {
this.backgroundMode.enable();
this.timerCounter = 0;
const objThis = this; //store this.
this.timer = setInterval(() => {
if (objThis.timerCounter === objThis.setTime) {
//onesignal notification
clearInterval(objThis.timer);
} else {
++objThis.timerCounter;
}
}, 1000);
this.backgroundMode.on('activate').subscribe(() => {
setInterval(function () {
this.startTimerCounter();
}, 240000);
})
}
}
use this plugin: cordova-plugin-background
使用这个插件: cordova-plugin-background
document.addEventListener('deviceready', function () {
cordova.plugins.backgroundMode.enable();
cordova.plugins.backgroundMode.onactivate = function () {
setInterval(function () {
YourFunctionHere();
}, 300000);
}
}, false);
your app it' well refrech in backround every 5 Min您的应用程序每 5 分钟在后台刷新一次
300000 millisecond = 5 min 300000 毫秒 = 5 分钟
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.