-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathclass_quicktimer.ahk
68 lines (67 loc) · 1.44 KB
/
class_quicktimer.ahk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
class quickTimer {
; User methods
__new(callback, priority:=0){
this.callback := callback
this.priority := priority
quickTimer.timers[this] := "" ; store the instance for xxxAll() methods
}
; Timer states:
; 1 - running
; 0 - stopped
; -1 - deleted (or never started)
state := -1
start(){
if this.state == 1
return
setTimer(this.callback, 0, this.priority)
this.state := 1
local pte := quickTimer.enabled++
if !pte
quickTimer.toggleQT()
return this
}
stop(){
if this.state != 1
return
this.state := 0
quickTimer.enabled--
setTimer(this.callback, "off")
}
delete(){
if this.state == 1
this.stop()
this.state := -1
setTimer(this.callback, "delete")
quickTimer.timers.delete this
this.base := "" ; will throw error on further use.
}
startAll(){
this.loopTimers(quickTimer.start)
}
stopAll(){
this.loopTimers(quickTimer.stop)
}
deleteAll(){
this.loopTimers(quickTimer.delete)
}
; Interal methods
static timers := []
loopTimers(fn){
local
global quickTimer
for timer in quickTimer.timers
fn.call(timer)
}
static enabled := 0
toggleQT(){
static tfn := quickTimer.quickFn.bind(quickTimer)
setTimer(tfn, "-0")
}
static speed := -1 ; fastest. Set to -1 for fastest, 0 for fast or 1+ for 'quite' slow
quickFn(){
static dllsleep := func("dllcall").bind("Sleep", "uint")
critical false
while this.enabled
(this.speed == -1) ? "" : dllsleep.call(this.speed), sleep(-1)
}
}