Perspective Timer

Hello all,
I'm needing to display a timer within a perspective session. I have a session property "MissionTime". When a button is pressed, I want it to start incrementing a timer to that property, then I can use the session property to bind displays and labels to.
I have tried a few things, one being-

self.session.custom.MissionTime = now()

Then do some math on that display to show a timer that started when the button is pressed. A different button press would stop the timer, and set "MissionTime" back to 0.

So far haven't been able to make this work at all, and some searching is yielding no good solutions for timers in perspective. Anyone have suggestions on how I could make this happen? Because of how dynamic my displays/sessions are, I need to make it happen without using tags.
Thanks all!

Make two session properties: MissionTimeStart and MissionTimeElapsed.

Write the timestamp of start button press to MissionTimeStart. Have a binding on MissionTimeElapsed that calculates difference between value returned by now() and the timestamp stored in MissionTimeStart.

On stop button press, write a null to MissionTimeStart. Have your binding on MissionTimeElapsed write a 0 if it sees a null in MissionTimeStart, or make it default to a value of 0 if the expression fails(which it will since it can't do datetime math on a null).

example expression for MissionTimeElapsed:

try(dateDiff(this.custom.MissionTimeStart, now(), 'second'), 0)

I like that idea a lot. I haven't been able to successfully write now() to a session property for some reason.
Any idea why self.session.custom.MissionTime = now() isnt working for that?

When you say you can't write now() to a session property, are you trying to write it via a binding or a python script?

for the onActionPerformed script on the button,

self.session.custom.MissionTimeStart = system.date.now()

should work.

1 Like

Looks like you are mixing expression functions and jython syntax. An expression binding would be just now(0). In a script, you'd do this, presumably inside an if block:

self.session.custom.MissionTime = system.datetime.now()

Heh: @ryan.white beat me by two minutes.

Everyone beat me, but here's a view to play with. All expressions, no scripts.
timer_2023-03-16_1457.zip (5.3 KB)

2 Likes

and @ryan.white Thank you. I was missing part of that. I gave it a try using

try(dateDiff(this.custom.MissionTimeStart, now(), 'second'), 0)

as a transform script and it was not coming out correctly.

This is a fanatic example. Thank you very much! Almost exactly what I am looking for, and I will be able to easily adapt it.