我正在使用恢复和暂停事件来知道应用程序何时在后台和前台运行。这里tm是当前系统时间,stym和etym是开始和结束时间。我希望我的恢复和暂停事件在stym和etym之间运行。当条件变为假时,恢复和暂停事件应停止。
const promise = new Promise(() => {
if ((tm >= stym) && (etym >= tm)) //condition for start and end time
{
this.platform.resume.subscribe((e) => {
this.resumetime = new Date();
alert(this.resumetime);
alert("resume called");
});
if(etym >= tm){
this.platform.pause.subscribe((e) => {
this.pausetime = new Date();
alert(this.pausetime);
alert("pause called");
});
}
else{
this.platform.pause.unsubscribe();
}
this.time = (this.resumetime - this.pausetime) / 60000; // time b/w pause and resume
alert(this.time);
this.prall = this.prall + this.time; // total sum of resume and pause
this.parray = [this.pausetime];
this.rarray = [this.resumetime];
var mn = new Date().getMinutes(); // getting presenttime minute
var min = this.pausetime.getMinutes(); // getting pause time minute
if ((mn - min) > 2) // if pause is more than 2 min
{
this.localNotifications.schedule({
id: 1,
title: 'Attention',
text: 'come back and continue your study',
trigger: { at: new Date(new Date().getTime() + 5 * 1000) },
data: { mydata: 'reminder for you' },
led: 'FF0000',
vibrate: true
});
}
}
else {
console.log("over");
this.platform.resume. unsubscribe();
this.platform.pause.unsubscribe();
}
})
promise.then(() => {
// here
console.log("promise then");
var sve = {
pause: this.pausetime, resume: this.resumetime, id: data.data._id,
prall: this.prall, parray: this.parray, rarray: this.rarray
}
// save pause and resume time and prall
this.authService.respau(sve).subscribe(data => {
if (data.success) {
alert('success');
} else {
alert('error');
}
})
});
promise.catch((err) => {
console.log("error");
});