[英]Cordova call function when NOT from resume
I am listening for deviceready
and resume
events in Cordova. 我正在听设备
deviceready
并在Cordova中resume
事件。
In my deviceready
I only want to call a function, if the app is not starting from a resume
. 在我的设备
deviceready
,如果应用不是从resume
启动的,我只想调用一个函数。
Ie can I achieve the below? 即我可以实现以下目标吗?
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
document.addEventListener("resume", onResume, false);
doOnlyWhenNotFromResume();
}
function onResume(event) {
doOnlyWhenFromResume(event);
}
Cordova version 7.1.0 Cordova版本7.1.0
You could use a boolean flag to avoid the doOnlyWhenNotFromResume()
function from being called on resume. 您可以使用布尔值标志来避免在简历上调用
doOnlyWhenNotFromResume()
函数。 If you set this flag when a pause
event happens it should work because after pausing the app and "warm-starting" the app again the resume event is triggered. 如果您在发生
pause
事件时设置此标志,则它应该起作用,因为在暂停应用程序并“热启动”应用程序之后,再次触发了恢复事件。
Declare this variable in some scope where its accessible for your functions: 在某个范围内声明此变量,以供您的函数访问:
var isResume = false;
And modify your existing code as follows: 并按如下所示修改您的现有代码:
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
document.addEventListener("resume", onResume, false);
document.addEventListener("pause", onPause, false);
if (!isResume) {
doOnlyWhenNotFromResume();
}
}
function onResume(event) {
doOnlyWhenFromResume(event);
}
function onPause(event) {
isResume = true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.