[英]execute a function each time, when app started in ionic
I am using ionic framework to build my app, which seems working fine as per my requirement. 我正在使用离子框架来构建我的应用程序,按照我的要求,它似乎可以正常工作。
Now i close my app and reopen again it maintaining the state which is also fine. 现在我关闭我的应用程序,然后再次重新打开它,保持状态也很好。
Now my question is 现在我的问题是
1) Does app.js files run each time when my app is open ? 1)app.js文件每次打开时都运行吗? i tried to add alert in app.js , which works only first time
我试图在app.js中添加警报,仅在第一次工作
Is this right or wrong ? 这是对还是错?
2) I want to run a particular function each time when my app get started. 2)我想在每次启动我的应用程序时运行一个特定的功能。 Is there is any way to do this ?
有什么办法可以做到这一点?
Thanks 谢谢
There is a very large documentation which gets upgraded every time a new major version of Cordova/Phonegap is released. 每次发行新的主要版本的Cordova / Phonegap时,都会更新大量的文档。 You can find that documentation here: Cordova Documentation 5.0
您可以在此处找到该文档: Cordova文档5.0
It describes an event which is called every time your "device is ready". 它描述了每次“设备准备就绪”时都会调用的事件。 It is called
onDeviceReady
. 它称为
onDeviceReady
。 To use this event you need an onDeviceReady-EventListener. 要使用此事件,您需要一个onDeviceReady-EventListener。 The documentation for the EventListeners can be found here: Events in Cordova
可以在这里找到EventListeners的文档: Cordova中的事件
You can add that EventListener with this command: 您可以使用以下命令添加该EventListener:
document.addEventListener("deviceready", yourCallbackFunction, false);
Like Zain described in the comments, there is a difference between exiting the application and pausing it, there is also another EventListener which gets called when the user pauses the application. 就像注释中描述的Zain一样,退出应用程序和暂停应用程序之间是有区别的,还有另一个EventListener在用户暂停应用程序时被调用。 It can be attached to your application with:
它可以通过以下方式附加到您的应用程序:
document.addEventListener("pause", yourCallbackFunction, false);
So you could create a function which gets called when those two listeners are fired like this: 因此,您可以创建一个在触发这两个侦听器时调用的函数,如下所示:
onDeviceReady onDeviceReady
document.addEventListener("deviceready", deviceIsReady, false);
function deviceIsReady() {
alert('Your device is ready!');
}
pause 暂停
document.addEventListener("pause", onPause, false);
function onPause() {
alert('Your application is paused');
}
Alternatively, if the alert is not called when the application is paused, you could add the Cordova Plugin Console and call the onPause
function with this content: 或者,如果在应用程序暂停时未调用警报,则可以添加Cordova插件控制台并使用以下内容调用
onPause
函数:
function onPause() {
console.log('Your application is paused');
}
Please let me know, if this solves your question or if you need further assistance. 如果这可以解决您的问题或需要进一步的帮助,请告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.