Last active
April 21, 2021 01:04
-
-
Save Generhr/e5c35c523596070ec5b5456fc77d1878 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Class Timer { | |
Static Instances := [] | |
__New(callback, interval := 0, priority := 0) { | |
Local | |
instance := {"Callback": callback, "Interval": interval, "Priority": priority | |
, "State": -1 | |
, "Base": this.__Timer} | |
pointer := &instance | |
, this.Instances[pointer] := instance, ObjRelease(pointer) ;* Decrease this object's reference count to allow `__Delete()` to be called while still keeping a copy in `Timer.Instances`. | |
if (interval >= 0 && interval == Round(interval)) { | |
instance.Start(interval) | |
} | |
return (instance) | |
} | |
StartAll(interval := "") { | |
Local | |
for pointer, object in this.Instances { | |
object.Start(interval) | |
} | |
} | |
StopAll() { | |
Local | |
for pointer, object in this.Instances { | |
object.Stop() | |
} | |
} | |
Class __Timer { | |
__Delete() { | |
if (this.State != -1) { | |
SetTimer(this.Callback, "Delete") | |
} | |
; if (Debug) { | |
; MsgBox("__Timer.__Delete(): " Timer.Instances.Count) | |
; } | |
pointer := &this | |
, ObjAddRef(pointer), Timer.Instances.Delete(pointer) ;* Increase this object's reference count before deleting the copy stored in `Timer.Instances` to avoid crashing the calling script. | |
} | |
Start(interval := "") { | |
Local | |
if (this.State != 1) { | |
this.State := 1 | |
if (interval != "") { | |
this.Interval := interval | |
} | |
SetTimer(this.Callback, this.Interval, this.Priority) | |
} | |
} | |
Stop() { | |
Local | |
if (this.State == 1) { | |
this.State := 0 | |
SetTimer(this.Callback, "Off") | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment