简体   繁体   English

每次应用程序以离子形式启动时,每次执行一个函数

[英]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.

相关问题 Android 每次启动应用程序时运行一段代码,但在应用程序最小化和重新打开时不运行 - Android run a piece of code each time the app is started but not when the app is minimised and reopened BroadcastReceiver每次启动服务时都会收到 - BroadcastReceiver receives when service is started each time Flutter - 当应用程序处于后台时,如何在特定时间执行 function? - Flutter - How to execute a function at a specific time when the app is in background? 再次启动时,应用崩溃 - App crashes when started again for a second time Android 6:应用每次启动时使用更多内存 - Android 6: App uses more memory each time it's started 加载应用程序时的离子调用功能 - Ionic call function when app is loaded 调用函数更新App时Ionic Force App Update崩溃 - Ionic Force App Update crashes when calling the function to update the App 首次在手机上运行Ionic应用程序时无法加载键盘吗? - Failed to load keyboard when running Ionic app on phone for first time? 启动应用程序时发送通知 - Notification is sent when app is started 当Crashlytics开始时,应用程序崩溃了 - App crashes when Crashlytics started
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM