Below is an example of a timer I have used a lot, it needs two variables; an app variable which is formula = NOW() and a page variable created from a button click which starts the timer which is also a formula = ADD_DURATION(NOW(), 0, “seconds”).
The formula below creates a display in the format 00:00:000
LEFT_PAD(STRING(GET_DATETIME_COMPONENT(DATETIME(DATETIME_DIFFERENCE(appVars.TimeNow,pageVars.TimerStart)),“minute”)),2,“0”) + " : " + LEFT_PAD(STRING(GET_DATETIME_COMPONENT(DATETIME(DATETIME_DIFFERENCE(appVars.TimeNow,pageVars.TimerStart)),“second”)),2,“0”) + " : " + LEFT_PAD(STRING(GET_DATETIME_COMPONENT(DATETIME(DATETIME_DIFFERENCE(appVars.TimeNow,pageVars.TimerStart)),“millisecond”)),3,“0”)
It works fine but my question is about the milliseconds. At the start, they are blistering fast, it looks like a real stopwatch but as the timer progresses, what displays for the milliseconds becomes less and less (per second). If you test it, you will see. Just wondering why that is? It is still accurate enough and milliseconds are perhaps overkill anyway, but it is just slightly odd looking that the timer seems to get tired (for milliseconds anyway).
Slightly obscure, but considering I have seen other people ask questions about timers lately, I thought I would throw this method out there and also finally ask about this which has always perplexed me.
Hey, I posed this a while ago but it was not clear there is a question at the end…it is something that annoys me more than anything…everything still works.
Can you tell me why the timer would slow down? It actually does it for seconds too…slows down sometimes and starts skipping some seconds…
I get the opposite, i have a timer, that is skipping 4-5 numbers and running faster.
i think, this might happen, because of overloading the app, to tell you the truth, i havent found time to look for the problem and solve it yet.
@Paul_Mc Hmm, not sure about the millisecond slowing but I would try to simplify your formula into something like the following (likely contains typos so please check the formula before use), see if this one would not slow?
FORMAT_DATETIME_LOCAL(ADD_DURATION("2022-02-02T00:00:00Z",DATETIME_DIFFERENCE(NOW(), pageVars.TimerStart)),“millisecond”)), "mm:ss:SSS")
I think for any timer I would always reference the system time and then set a stop point based on that. If there is any sort of local issue it will always correct itself. You can never control what is going on inside a device once your app installed.
This reduces the issues to skipping, but leaves accuracy unaffected.
My formulas evolved so much from getting them to work, then tweaking them to display nicely…they needed a review! Adapting your way, I ended up with:
FORMAT_DATETIME_LOCAL(ADD_DURATION(DATETIME(0),DATETIME_DIFFERENCE(appVars.TimeNow, pageVars.TimerStart)),“mm : ss : SSS”))
Much neater, I have both the old and new timers on the screen. I will see if they are different as the old timer ‘slows down’…
A final point on this…I am giving up
The timer though still slows down the longer it runs. The excerpt above is just that; the actual formula in my app is longer and is composed of IF functions that ensures that at the start it shows 00 : 00 : 000 and after a point in time, the timer stops and shows the end time. Anyway, I thought this IF construction was causing it too hang but if I do a simple timer with just the above, it still slows down. Weird but not the end of the world. If any one else is interested in the structure;
where appVar.TimeNow = NOW()
pageVar.TimerStart = ADD_DURATION(NOW(), 7, “seconds”). So the timer starts after the page is focused…
Hi! In what timeframe do you get the slowing down? I did a test on this but couldn’t notice any slowing down in several minutes at least (testing on web). My setup was as follows:
My timers usually run for about 20 mins, and i see it well into it, say after 10 minutes…
Hmmm, tried leaving the timer running but it still looks like it’s working perfectly? (tested on web) Please check out the logic I used and see if that would work better for you?
Appreciate your help on this. I have replicated your set up, only difference is I linked to ‘Page focused’ rather than ‘Page mounted’ and it still slows down.
The top timer below is my original one using an app variable, the second one is my setup (with additional IF formulas for the start and end of the timer) amended with your set up, and the third is your set up with nothing else in the formula.
They all slow down…really odd!
I recommend using page mounted so it does not fire more than once Since if page focused has fired several times, there are several loops going on and that might bog your timer down.
One of the things that confused me initially was ‘Page Foucused v Page Mounted’ and still not sure I really understand it.
On the timer, I tested your way and I think it is better, but what you are suggesting would involve me rebuilding lots of formulas across my app. I am going to park this for now, and maybe do on a next release
Thanks for your help.
This is really cool – it works great Thanks
The only very small addition I made was to use a monospace font, and add buttons to start and stop the timer. On the start button I add the formula to reset the time_start variable.
P.S.: You should have a challenge where its like the game where everyone as a group tells a story, but each person adds 1 sentence to the previous person’s sentence. Same with an app challenge.
Think of the Page Mounted event as happening only once when the Page is first loaded, and not firing again until after it is unloaded (such as with Replace Page flow function) and then opened again.
So there you have a Page loaded in memory, even if you use Open Page to view other pages (unless you use Replace Page). It remains “Mounted” in memory.
The Page Focus event fires every time the Page becomes visible or gains input focus. This happens every time you navigate back to the Page or Open it for the first time.
Thanks John, that helps.
I don’t use ‘Replace Page’ at all…I used to and cannot remember why (think it was because I also use ‘Navigate Back to Root’ which doesn’t work with ‘Replace Page’), but I removed them all… But anyway, because I need the timer starting from zero every time the page opens, I need to use Page Focus because if someone returns to the page having mounted it earlier, I need the timer to start again…rather than continue from the first visit/mounting.