TimeSinceLastCall()

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
tidbit
Posts: 1272
Joined: 29 Sep 2013, 17:15
Location: USA

TimeSinceLastCall()

06 Nov 2013, 13:38

TimeSinceLastCall() AHK 1.1+
Get the amount of time, in milliseconds, since the last time you have called this function.
You may have multiple timers using the ID parameter, defaults to 1.
You may reset all timers or a single one using the reset parameter.

Demo:

Code: Select all

; Demo:
j::ToolTip, % ggg.="1 :: " timeSinceLastCall(1) "`n"
k::ToolTip, % ggg.="2 :: " timeSinceLastCall(2) "`n"
l::ggg:="", timeSinceLastCall(0,1) ; reset everything
esc::ExitApp
Function:

Code: Select all

/*
timeSinceLastCall ( http://ahkscript.org/boards/viewtopic.php?f=6&t=537 )
   Return the amount of time, in milliseconds, that has passed since you last called this function.

   id    = You may use different ID's to store different timesets. ID should be 1 and above (not 0 or negative) or a string.
   reset = If reset is 1 and id is 0, all ID's are cleared. Otherwise if reset is 1, that specific id is cleared.

   * NOTE:
   The first call is usually blank.

example:
   out:=timeSinceLastCall()
   sleep, 500
   out:=timeSinceLastCall()
output:  500
*/
timeSinceLastCall(id=1, reset=0)
{
   static arr:=array()
   if (reset=1)
   {
      ((id=0) ? arr:=[] : (arr[id, 0]:=arr[id, 1]:="", arr[id, 2]:=0))
      return
   }
   arr[id, 2]:=!arr[id, 2]
   arr[id, arr[id, 2]]:=A_TickCount      
   return abs(arr[id,1]-arr[id,0])
}
rawr. fear me.
*poke*
Is it December 21, 2012 yet?
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: TimeSinceLastCall()

07 Nov 2013, 19:22

I have a function that does something similar in a more robust and versatile way.

It is primarily for creating Timers of various lengths and then checking for if that much time has passed yet. But once the timers gets to zero and the allotted amount of time has passed the timer returns a negative number showing the amount of time that has passed since the timer finished.

http://ahkscript.org/boards/viewtopic.p ... 4635#p4635

Code: Select all

Timer("TEST",1)  ; create timer called TEST with a 1 millisecond Period (option structure does not allow the creating of a timer of zero Period)

Loop
	MsgBox % Timer("TEST","L")  ; returns the time Left which is basically a negative number of how long ago the timer was started
TimeSinceLastCall is elegant in its simplicity. Just showing another function that is along the same vein that uses a similar technique of putting A_TickCount information into an array.

EDIT:
I liked the idea of an elapsed time so made some changes to my function to allow this also with an "Elapse" option and also rewrote the structure of how the function options were handle to allow creating timers with a 0 duration.

Resulting in allowing this now

Code: Select all

Timer("TEST",0)  ; create timer called TEST starting with a countdown time of zero (this could be any number as it is irrelevant when using the "Elapse" option

Loop
	MsgBox % Timer("TEST","E")  ; returns the time Elapsed since timer TEST was started
Keeping track of a lot of timers is very important in many gaming scripts but also useful in non-gaming scripts from time to time.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
User avatar
joedf
Posts: 8951
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: TimeSinceLastCall()

07 Nov 2013, 23:15

simplo-neato! :D
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
carno
Posts: 265
Joined: 20 Jun 2014, 16:48

Re: TimeSinceLastCall()

19 Sep 2018, 06:55

FanaticGuru wrote:I have a function that does something similar in a more robust and versatile way.

It is primarily for creating Timers of various lengths and then checking for if that much time has passed yet. But once the timers gets to zero and the allotted amount of time has passed the timer returns a negative number showing the amount of time that has passed since the timer finished.

http://ahkscript.org/boards/viewtopic.p ... 4635#p4635

Code: Select all

Timer("TEST",1)  ; create timer called TEST with a 1 millisecond Period (option structure does not allow the creating of a timer of zero Period)

Loop
	MsgBox % Timer("TEST","L")  ; returns the time Left which is basically a negative number of how long ago the timer was started
TimeSinceLastCall is elegant in its simplicity. Just showing another function that is along the same vein that uses a similar technique of putting A_TickCount information into an array.

EDIT:
I liked the idea of an elapsed time so made some changes to my function to allow this also with an "Elapse" option and also rewrote the structure of how the function options were handle to allow creating timers with a 0 duration.

Resulting in allowing this now

Code: Select all

Timer("TEST",0)  ; create timer called TEST starting with a countdown time of zero (this could be any number as it is irrelevant when using the "Elapse" option

Loop
	MsgBox % Timer("TEST","E")  ; returns the time Elapsed since timer TEST was started
Keeping track of a lot of timers is very important in many gaming scripts but also useful in non-gaming scripts from time to time.

FG
Thanks for the update. Great function that I can use immediately! :)

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 62 guests