Timer with individual event.time

Good day I need help on my timer

right now when I execute “event.time” with 2 of my timers, It displays the same.

sample:

local function timerA(event) print("TIMER A : ".. event.time) end timer.performWithDelay(1000, timerA, 0 ) local function timerB(event) print("TIMER B : ".. event.time) end local function startTimerB(event) timer.performWithDelay(1000, timerB, 0 ) end timer.performWithDelay(5000, startTimerB, 1 )

Both will have the same value. Even though Timer B has started late. How can I make both timers have it’s own “event.time”

I think that event.time returns the same as system.getTimer(), which is the number of milliseconds since the app was launched. 

The easiest way I can think of to work around this is by having your own variables for tracking the time between timer calls:

local totalTimeA = 0 local totalTimeB = 0 local lastTimeA = nil local lastTimeB = nil local function timerA(event) totalTimeA = totalTimeA + (event.time - lastTimeA) lastTimeA = event.time print("TIMER A : ".. totalTimeA) end lastTimeA = system.getTimer() timer.performWithDelay(1000, timerA, 0 ) local function timerB(event) totalTimeB = totalTimeB + (event.time - lastTimeB) lastTimeB = event.time print("TIMER B : ".. totalTimeB) end local function startTimerB(event) lastTimeB = system.getTimer() timer.performWithDelay(1000, timerB, 0 ) end timer.performWithDelay(5000, startTimerB, 1 )

totalTimeA/B will be the length of time that timer has been running. 

lastTimeA/B are used to record the current time so that it can be used in the next timer call to work out the time difference between calls.

I ran your code, I’m getting different values for timer a and timer b:

Aug 22 02:46:59.896 TIMER A : 1127.539
Aug 22 02:47:00.905 TIMER A : 2135.819
Aug 22 02:47:01.913 TIMER A : 3143.921
Aug 22 02:47:02.921 TIMER A : 4151.809
Aug 22 02:47:03.929 TIMER A : 5159.935
Aug 22 02:47:04.904 TIMER B : 6135.791
Aug 22 02:47:04.936 TIMER A : 6167.117
Aug 22 02:47:05.912 TIMER B : 7143.789
Aug 22 02:47:05.944 TIMER A : 7175.813
Aug 22 02:47:06.921 TIMER B : 8151.918
Aug 22 02:47:06.952 TIMER A : 8183.546
Aug 22 02:47:07.928 TIMER B : 9159.487
Aug 22 02:47:07.959 TIMER A : 9190.931
Aug 22 02:47:08.936 TIMER B : 10167.873
Aug 22 02:47:08.967 TIMER A : 10198.716
Aug 22 02:47:09.944 TIMER B : 11175.801
Aug 22 02:47:09.976 TIMER A : 11207.795
Aug 22 02:47:10.952 TIMER B : 12183.807
Aug 22 02:47:10.983 TIMER A : 12214.881
Aug 22 02:47:11.959 TIMER B : 13190.735
Aug 22 02:47:11.992 TIMER A : 13223.805
Aug 22 02:47:12.968 TIMER B : 14199.848
Aug 22 02:47:12.999 TIMER A : 14230.922
Aug 22 02:47:13.976 TIMER B : 15207.89
 

@Rob Miracle, yep they are different but, my desired output is like this, timer B should have a starting value of zero, and timer A should continue

@Alan QuizTix, your code works, but it appears that It is not good for a shared function.

I think that event.time returns the same as system.getTimer(), which is the number of milliseconds since the app was launched. 

The easiest way I can think of to work around this is by having your own variables for tracking the time between timer calls:

local totalTimeA = 0 local totalTimeB = 0 local lastTimeA = nil local lastTimeB = nil local function timerA(event) totalTimeA = totalTimeA + (event.time - lastTimeA) lastTimeA = event.time print("TIMER A : ".. totalTimeA) end lastTimeA = system.getTimer() timer.performWithDelay(1000, timerA, 0 ) local function timerB(event) totalTimeB = totalTimeB + (event.time - lastTimeB) lastTimeB = event.time print("TIMER B : ".. totalTimeB) end local function startTimerB(event) lastTimeB = system.getTimer() timer.performWithDelay(1000, timerB, 0 ) end timer.performWithDelay(5000, startTimerB, 1 )

totalTimeA/B will be the length of time that timer has been running. 

lastTimeA/B are used to record the current time so that it can be used in the next timer call to work out the time difference between calls.

I ran your code, I’m getting different values for timer a and timer b:

Aug 22 02:46:59.896 TIMER A : 1127.539
Aug 22 02:47:00.905 TIMER A : 2135.819
Aug 22 02:47:01.913 TIMER A : 3143.921
Aug 22 02:47:02.921 TIMER A : 4151.809
Aug 22 02:47:03.929 TIMER A : 5159.935
Aug 22 02:47:04.904 TIMER B : 6135.791
Aug 22 02:47:04.936 TIMER A : 6167.117
Aug 22 02:47:05.912 TIMER B : 7143.789
Aug 22 02:47:05.944 TIMER A : 7175.813
Aug 22 02:47:06.921 TIMER B : 8151.918
Aug 22 02:47:06.952 TIMER A : 8183.546
Aug 22 02:47:07.928 TIMER B : 9159.487
Aug 22 02:47:07.959 TIMER A : 9190.931
Aug 22 02:47:08.936 TIMER B : 10167.873
Aug 22 02:47:08.967 TIMER A : 10198.716
Aug 22 02:47:09.944 TIMER B : 11175.801
Aug 22 02:47:09.976 TIMER A : 11207.795
Aug 22 02:47:10.952 TIMER B : 12183.807
Aug 22 02:47:10.983 TIMER A : 12214.881
Aug 22 02:47:11.959 TIMER B : 13190.735
Aug 22 02:47:11.992 TIMER A : 13223.805
Aug 22 02:47:12.968 TIMER B : 14199.848
Aug 22 02:47:12.999 TIMER A : 14230.922
Aug 22 02:47:13.976 TIMER B : 15207.89
 

@Rob Miracle, yep they are different but, my desired output is like this, timer B should have a starting value of zero, and timer A should continue

@Alan QuizTix, your code works, but it appears that It is not good for a shared function.