View Full Version : Time app running?
Jonas DK
09-23-2005, 05:34 PM
Is there a way to record how long the app have been running?
I know I can use the page.OnTimer to have it call the system time every minut or so but that calculation will be reset every time you leave or enter the page.
I want to be able to display a timer in the project that tells people for how long they have been using the app. and I want the timer to pause when the app looses focus.
I know all these things are posible seperatly but how to get them to work together.....
anyone got any ideas?
cheers,
Jonas DK
Corey
09-23-2005, 05:54 PM
Hi. Couple ways:
1. Check/compare the system time.
2. Use the timer and just add a "totalTime" variable assignment to it, so for example:
Page.StartTimer(1000);
Then in the timer area have something like this:
totalSeconds = totalSeconds + 1;
That variable's value is retained across different pages. Then you can easily translate that to minutes as needed, i.e.
totalMinutes = totalSeconds/60;
:yes
Jonas DK
09-23-2005, 06:01 PM
of cause...
its so simple....
. o O (must be getting tired.. its 01:00 here)
but what about pausing when loosing focus.. now that is tricky I tried some different things of what I could come up with but non of it works.
Corey
09-23-2005, 06:09 PM
I'm not really adept at hooking windows, etc. because I rarely do that but it's definitely doable. I believe the gist of that would be to sandwich your variable incrementing inside of an IF statement which checks to see if your window has focus, so it's only incremented in that case.
To create the IF statement which checks to see if your window is focused, you'll need some help from one of the more advanced scripters like Brett or Worm. It's not super hard or anything but I think those are the guys with the ticket on that one. Search around the forum a bit, seems ot me I saw an example here once on checking for focus. :yes
Jonas DK
09-23-2005, 06:22 PM
Hi. Couple ways:
1. Check/compare the system time.
2. Use the timer and just add a "totalTime" variable assignment to it, so for example:
Page.StartTimer(1000);
Then in the timer area have something like this:
totalSeconds = totalSeconds + 1;
That variable's value is retained across different pages. Then you can easily translate that to minutes as needed, i.e.
totalMinutes = totalSeconds/60;
:yes
I added this to the ontimer
strTimersec = strTimersec + 1;
if strTimesec >= "60" then
OnlineTime = strTimersec/60;
end
Paragraph.SetText("parTimer", "Du har i dag været online: "..OnlineTime.." Minuter");
But I get an error in line 2 about trying to compare to nill
problem is that if I let it update every secound it wont write out the entire paragraph and the text will be flashing...
Jonas DK
09-23-2005, 06:35 PM
forget my last post..
What if I only want it to update the paragraph every minute?
Corey
09-23-2005, 07:15 PM
Couple ways. You can run the timer every second and use a flag variable, this works best if you have other script segments relying on that timer, or you can just set the timer to run once per minute if nothing else is relying on it. So to use a flag variable you would add something like this to your timer:
if totalSecs<60 then
totalSecs = totalSecs + 1;
else
totalMins = totalMins + 1;
totalSecs = 0;
end
Or for the second choice, to run your page timer once per minute, just use this:
Page.StartTimer(60000);
:) :yes
Jonas DK
09-23-2005, 07:26 PM
nice one
think I'm going to use that flag one...
for now I just used math.round to eleminate the enourmes amount of decimals then counting in minutes, that stops the text from flashing and works the same way as your flag variable will properbly do, but for the coding of it the flag variable is nicer...
Corey
09-23-2005, 07:27 PM
Perfect. :) :yes
Jonas DK
09-23-2005, 07:31 PM
but changing the code will have to wait a couple of hours
We are now comming dangeresly close to saturday morning (here it is 02:30), aprocing the 24 hour mark.
be back in a few hours time... :o
Put this in your apps Preload event:
basetime = String.ToNumber(DLL.CallFunction(_SystemFolder .. "\\winmm.dll", "timeGetTime", "", DLL_RETURN_TYPE_LONG, DLL_CALL_STDCALL));
then whenever you want to know how much time has passed since the app started, do this:
curtime = String.ToNumber(DLL.CallFunction(_SystemFolder .. "\\winmm.dll", "timeGetTime", "", DLL_RETURN_TYPE_LONG, DLL_CALL_STDCALL));
--get the elapsed time in miliseconds
elapsedtime = curtime - basetime;
Jonas DK
09-23-2005, 07:37 PM
Worm you are a life saver...
Cheers mate
Corey
09-23-2005, 07:37 PM
Sweet! :) :yes
Jonas DK
09-24-2005, 05:21 AM
Is it posible to start the Page.Timer from a project global like Project.OnStartup
or do I have to set Page.StartTimer on every page?
TJ_Tigger
09-24-2005, 08:47 AM
Is it posible to start the Page.Timer from a project global like Project.OnStartup
or do I have to set Page.StartTimer on every page?
The Timer is a page specific action. If your user is to be navigating between pages within your app then you will need to restart the timer everytime they jump pages.
On the otherhand, you can set a base time using Worm's method above on Project startup and then calculate the total running time on project shutdown.
Tigg
Jonas DK
09-24-2005, 08:54 AM
The Timer is a page specific action. If your user is to be navigating between pages within your app then you will need to restart the timer everytime they jump pages.
On the otherhand, you can set a base time using Worm's method above on Project startup and then calculate the total running time on project shutdown.
Tigg
Yes but I'm using it to update a paragraph every minute, so the user can keep track of the time.
like this:
TJ_Tigger
09-24-2005, 09:14 AM
Yes but I'm using it to update a paragraph every minute, so the user can keep track of the time.
like this:
Not a problem start the timer on the page and use a function like this on the On Timer event.
function DoTimer(strLabel)
if basetime == nil then
basetime = DLL.CallFunction(_SystemFolder .. "\\winmm.dll", "timeGetTime", "", DLL_RETURN_TYPE_LONG, DLL_CALL_STDCALL)
end
curtime = DLL.CallFunction(_SystemFolder .. "\\winmm.dll", "timeGetTime", "", DLL_RETURN_TYPE_LONG, DLL_CALL_STDCALL)
elapsedtime = ((String.ToNumber(curtime) - String.ToNumber(basetime)) / 1000)
hours = Math.Floor(elapsedtime / 3600);
mins = Math.Floor((elapsedtime - (hours * 3600)) / 60);
secs = Math.Floor(elapsedtime - ((hours*3600)+(mins*60)));
if hours < 10 then
hours = "0"..hours;
end
if mins < 10 then
mins = "0"..mins;
end
if secs < 10 then
secs = "0"..secs;
end
Label.SetText(strLabel, hours..":"..mins..":"..secs)
end
DoTimer("Label1")
Tigg
Jonas DK
09-24-2005, 09:25 AM
well I used this function in the global function:
function InitTimer()
if Timesec<60 then
Timesec = Timesec + 1;
else
curtime = String.ToNumber(DLL.CallFunction(_SystemFolder .. "\\winmm.dll", "timeGetTime", "", DLL_RETURN_TYPE_LONG, DLL_CALL_STDCALL));
elapsedtime = curtime - basetime;
OnlineSec = elapsedtime/1000;
OnlineMin = OnlineSec/60;
OnlineTime = Math.Round(OnlineMin, 0);
if OnlineTime == 1 then
Paragraph.SetText("parTimer2", OnlineTime.." Minut");
Timesec = 0;
else do
Paragraph.SetText("parTimer2", OnlineTime.." Minuter");
Timesec = 0;
end
end
end
end
and then just call the function every secound from the page timer.
It is not as sofisticated as yours but it works..
cheers,
Jonas DK
TJ_Tigger
09-24-2005, 09:34 AM
As long as it works. That function wasn't mine either, it came from Worm too.
Tigg :D
Lorne
09-26-2005, 02:02 AM
You could also use the built-in lua functions like os.time and os.clock and such.
More info at the lua wiki (http://lua-users.org/wiki/OsLibraryTutorial).
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.