When you create applications in PowerApps quite quickly you might want to create things that run in the background. You could use Microsoft Flow for this, however for some applications this might just not do the trick as you will find your app waiting for a Flow to report back. In one of my recent projects I had multiple screens in my app that would hold all sorts of data related to documents. The data was stored in multiple lists and libraries and I wanted to make sure that all the data in all my screens would be up to date all the time. My interface in one screen would change depending on data in another screen.
In this case I found that the data updates (done by other applications) were not reflected in my application and I had to write some low code to update the data used by my application. I decided to use the timer control for this.
The Timer Control is actually one of the more complicated controls used in PowerApps. At the same time it is also a very useful control.
The OnTimerStart, OnTimerEnd and OnSelect are the configurations where you can add your code. In my example in this post I will simply set a variable, but in my earlier mention project I would refresh or reload all data needed by the current screen. This current screen is important! A timer is added as a control on a screen and it will only run when your screen is active. Unless … As I go through the configuration options you will find the unless option.
To control the running of your code there are 6 settings that are important
If you want to start a timer you can set the Start to true. This will trigger the OnStart event and your OnStart code is run. This means that within your PowerApps code (for example on a click of a button) you can start the timer to run. It will then run as long as you haven’t set the Start back to false.
The Duration is the time it takes for the timer start again. So for example if you set this to 6000. The timer will restart after 6000 miliseconds.
The repeat option gives you control over if you want the timer to restart after it runs. If you want the timer to stop after the first run or if you want it to continue for ever will decide if you set this to true or false.
Should your timer revert back to its original value? You might have noticed that the timer displays the time as it counts and it goes back to 0 every time it restarts.
Text(Time(0, 0, Timer2.Value/1000), “hh:mm:ss”)
If half way through the duration you can’t to force a reset then you can set this variable to true and the time will then stop.
The AutoStart setting needs to be set to true if you want to timer to automatically start. Form screen refresh features I set this to true so that my data is refreshed when the user goes to the screen holding the timer control.
The AutoPause setting needs to be set to true if you want to timer to automatically pause when a user leaves the current screen. if you set this to false then the timer will keep running. This might be one of those options where you initially think that you want to set this to false and have a single timer refresh all your data. Just imagine if you have a larger application, do you want to update all your data all the time,even if it isn’t looked at?
App OnStart considerations
I’ve seen many people use the app OnStart to set some default settings. rather than using the App Onstart you could also configure a Timer. Using a timer may have some benefits. Even if it is only because the App OnStart only runs when you first start the app. This included PowerApps Studio. To force the OnStart code to run you may have to close and re-select the edit option. Timers are easier in this case.
Also if anything fails in your app OnStart code, the user will have to restart the applications. When you use a Timer, you can simply try running the code. If something goes wrong inform the user and then retry the code after a while,without the user having to restart the app.
Timers might not always be the best option, but I have found that they very often make it easier (yes, it is a bit of coding sometimes) to control my data rather than using OnVisible and OnStart events