Uncategorized

How to use a timer across screens!

A person holding a pocket watch

Introduction - Need for a timer

I came across an interesting use case in my current project. The need was to check a user's inactivity on a screen and log them out automatically after 15 minutes. So the approach was simple, use a timer! The approach was to:

  1. Start a timer as soon as a user gets to a particular screen.
  2. Reset it if the user takes any action on the screen.
  3. At the end of its run, use the Exit() function to log the user out.

Issues with multiple timer controls

The timer control consumes a lot of resources. Once the user navigates through multiple screens, multiple such controls become active. With 30+ screens in the app (don't ask me why, that's a whole different topic altogether), the app was getting too slow. One of the most common suggestions when it comes to performance improvement is to try to repurpose controls. I was under the impression that I could not do that with a timer i.e. I cannot stop/start/reset it from a different screen.

Solution - Using one timer

To confirm this before making significant changes in the app, I did a simple test involving the following steps:

  1. Added the following controls on Screen1:
    • A timer with the following properties:
      1. AutoStart = true
      2. AutoPause = false
      3. Duration = 5000 (5 seconds)
      4. Repeat = false
      5. Reset = gblResetTimer
      6. Start = gblStartTimer
      7. OnTimerEnd = Set(gblTimerEndValue, true);  (to test out if the timer does what its supposed to at the end of its run)
    • A label to show it's value (Timer.Value)
    • A button to start it with its OnSelect set to: Set(gblStartTimer, false); Set(gblStartTimer, true);
    • A button to navigate the user to a 2nd screen with its OnSelect set to: Navigate(Screen2)
    • A label to display the value of gblTimerEndValue
  2. Set the OnVisible of Screen1 to: Set(gblTimerEndValue, false);
  3. Added the following controls on Screen2:
    • A label to show the timer's value (Timer.Value)
    • A button to start the timer with its OnSelect set to: Set(gblTimerEndValue, false );Set(gblStartTimer, false );Set(gblStartTimer, true );
    • A button to reset it with its OnSelect set to: Set(gblResetTimer, false); Set(gblResetTimer, true);
    • A label to display the value of gblTimerEndValue
    • A button to navigate the user to the 1st screen with its OnSelect set to: Navigate(Screen1)

Here is a quick demo of my test:

A timer control being repurposed on 2 screens
A timer control being repurposed on 2 screens

As you can see, I can capture a it's value on the 2nd screen. I can also reset and start it from the 2nd screen.

Pro Tip: This doesn't work if there is no reference to the timer on the 2nd screen. In my example above, I have a label that is displaying the timer's value. However, this creates cross screen dependencies.  Resetting the timer on the OnVisible of the 2nd screen using the Reset function removes this dependency in the app checker.

Recent articles

  1. How to fix common iOS UX issues
  2. A-Z: Learn how to improve your Power Apps with 26 tips, again!
  3. How to default a SharePoint person column

2 thoughts on “How to use a timer across screens!”

Leave a Reply