How to create a timer that runs in the background or with the app closed completely

Hey Thunkers!

I wanted to create a quick tutorial to demonstrate how to have a timer that runs in the background independent of the app being open or closed.

First, here is the visual layout, of course, i doubt yours will be the same as mine exactly but for demonstration purposes, here you go!

Step 1: Create 1 stored variable. This guy will do our real “timer” work and keep track of everything we need

Because I utilize the variable as a list, I need to set it as such and because of the variable type, this will be done on the first load of the app. I check if ‘time’ = null (which it should if it’s the first time with the app) and set it to be a list with 10 items with a value of 0.

Also on load, I check Var Stored.Time #1, this is the position I use to indicate if the timer is running or not and label the button.StartStop as such. Then i set some labels, label.TotalTime (#4) label.StartTime (#5) if true, the app indicates to start the timer and if false, to show the last time the timer was started and label.EndTime (#6) displaying a very similar value to label.StartTime.

the next thing that occurs is the user presses the start button. because pos 1 = 0 from the start, the logic will execute this set of blocks on 1st press. this then sets Stored.Time #1 = 1 which means the timer is running. It sets Stored.Time #2 as seconds since1970 this is the start time and sets the button to “stop”. In Stored.Time #5 I store a clock value that makes sense to the human seeing it. This would be a number as seen on a digital 12 hour clock. and finally the label.ShowStartTime is set to display the start time

next the user can click stop timer. this sets Stored.Time #1 = 0 meaning timer is not running. Sets Stored.Time #3 as seconds since 1970 this is the stop time. now a function is used to calcualate and return a value to set Stored.Time #4 this is the total time in seconds Once this value is returned, Stored.Time #6 is set to be the end time in a 12 hr clock format and is then diplayed on label.ShowEndTime .

label.ShowTotalTime is set by dividing the Stored.Time #4 the total seconds that time has been being counted and divides by 60 to provide a value in minutes. `this could be further refined to show minutes and seconds. probably dividing by 60 1ith 0 decimal places and using the remainder~. lastly the button.StartStop is set to show “start Timer”


Finally, the user can reset the timer like this

Thanks for sticking around. You will find a copy of this in my Sandbox Environment here
Screen Shot 2020-03-14 at 11.59.28 PM

4 Likes

Cannot wait to try this, Jared you are LEGENDARY. :slight_smile:

2 Likes

Thanks! I just like to find solutions for my problems and this was one of them! I hope it helps!

1 Like

It will works in the background or not

Of course!

Hi, for the total time its showing “Nah min”

and when i try to change
like set text in label total that is start time - end time
then its showing “0 min”



This is an overly complicated solution I provided. I’d love to work out something new showing how to work with Jason to make your life easier. Give me a week I will provide a new example of how to keep a time running with the screen is close it’s a little easier to follow

1 Like