web-based timer

Hi, I have a web app with a built-in timer.

There are the following properties attached to the Session:

CurrentTime
FinishTime
SpentTime
SpentTimeMin
SpentTimeSec
SpSec
StartTime

All are public Integers except for SpSec which is a public String.

The Start button has the following code. It identifies the start time, sets the Finish time to 10 min after start, and switches on the timer.


Session.StartTime=Microseconds/1000000
Session.FinishTime=Session.StartTime +600
Timer1.Mode=2

The timer has the following code


Session.CurrentTime=Microseconds/1000000


Session.SpentTime=Session.CurrentTime - Session.StartTime
Session.SpentTimeMin=Session.SpentTime/60
Session.SpentTimeSec=Session.SpentTime -Session.SpentTimeMin*60

if Session.SpentTimeSec<10 then
  Session.SpSec="0" + str(Session.SpentTimeSec)
else
  Session.SpSec=str(Session.SpentTimeSec)
end

If Session.CurrentTime>=Session.FinishTime then
  Timer1.mode=0
end if

For some reason, the timer never stops at 10:00 sharp.

For a test, I opened 10 tabs and started this app in all of them. The timers stopped at different times in all of them ranging from 10:10 to 11:26.

Why does not the timer stop as soon as it reaches the FinishTime?

I would appreciate any advice.

Thank you,

Val

The timer class is not a precision system. Timer action occurs “after at least Period milliseconds have passed”.

The documentation for Timer.Period mentions that “The rate that a Timer can actually fire depends on the speed of the host computer, the operating system, and other tasks the computer is doing.”

If you need guaranteed time precision, you’ll need to find an approach other than setting the timer period for your desired FinishTime. There are multiple approaches, but I don’t feel comfortable making a recommendation without knowing what your use case is.

HI, Tim,

Yes, understand what you mean by saying that the timer is not accurate. In the previous iteration of the timer, I was counting the timer cycles, and it was very inaccurate.

With the code in the original post, the condition to stop the timer uses the Microseconds for the current time and and calculated finish time.

The finish time is calculated in the Start button “Action” event by adding 10 min to the start time. This has nothing to do with the timer.

The timer is supposed to stop as soon as current time is equal or more than the finish time.

The question is why it does not stop?

There could be other factors because of the ambiguous naming to Session or the fact that Timer.Action fires in a sessionless context. Honestly I would need an example project to find the exact reasoning.

My first post explains one of the most common gotchas to timers.

Just for confirmation are you talking Timer in a Webapp or WebTimer in a Webapp as a Timer in a webapp is server side and WebTimer in a webapp is client side. They are two different animals.

HI, Steve,
Thank you for pointing me to the differences between Timer and WebTimer. I am using the one that is instantiated via IDE and has WebTimer as its superclass. I believe it is a WebTimer that operates on the client’s side.
I am trying to learn about the difference between the two - can you guide me to the finger direction?
Thank you,
Val

Steve, please correct me if I am wrong.

Since my web app can run many instances in the browser, the timer should be on the browser side. Is this correct?

Since each web app instance need to have its own timer, all the properties should be put on the client’s side into the Session. Is this correct too?

Thank you in advance,

Val

Yes you would want to use WebTimer in your described situation.

I have added several properties to the Session:

d as DateTime
d1as DateTime
d2 as DateTime
interval as DateInterval

For the interval, I’ve put this code in the main area right below where it says interval As DateInterval


0,0,0,10,0,0

I hope this code means that the interval default value is 10 min

I’ve put this code into the button that starts the timer, so that the app knows at what time to stop running the timer


Session.d = DateTime.Now

Session.d1 = Session.d + Session.interval

The Session.interval produces NillException when I click the button during the run time.

Why does Session.interval produce NillException if it is explicitly declared via IDE?

The other properties such as d, d1, and d2 do not give me any trouble yet.

I am playing with this interval as DateInterval for three hours with no luck. Please help someone!

Thank you,

Val

What I understand that you are doing is something like this:

The area below “Interval as DateInterval” is just for comments or reference, no code that you put there will be executed.

That’s why your Interval is Nil, because in fact it is Nil.

If you need an interval for 10 minutes you need code like:

Session.Interval = New DateInterval(0, 0, 0, 0, 10)

Thank you, Alberto, Steve, and Tim,

I made the timer work. Now I need to create a test project and learn how to use DateTime and DateInterval.

Knowing how to do the same thing in several different ways is always good.

I appreciate you three helping me.

Val