此类依赖extooltip.ahk,extype.ahk
【2023/6/18】声明:此类已经重构,加入了循环timer,可以简单多线程执行脚本。不过自从遇到火灾抑郁后,写代码积极性大减,慢慢调试吧。
声明:
- 所贴出的代码纯属个人原创
- 所有代码经过测试才贴,但不保证所有功能最终完成,可能后续会有更新,欢迎随时回顾
- 所有非原创的代码,会声明,尽量给出引用出处
主要接口
- 主要把时间间隔基础定位基于秒(内置基于毫秒)
- 建立一个类,可以自由添加线程到时间队列
- 这个时间队列特地设定成顺序执行,确保鼠标操作不会同时执行而引起冲突
- 执行完动作后,会重新刷新计时器到正确的计时步骤,确保临近挂起的线程和后续线程能够执行
SecSub(BigTime,SmallTime)
SecAdd(Time,seconds)
DurationFormat(seconds)
DurationParse(durationString=="1d2h'3""4")
Now()
;//直接函数
AfterNow(secondsNumber)
AfterNow(durationString)
AfterTime(durationString="1d2h'3""4",function,params*)
;//这里注意,字符串内的双引号是要写2个才能转义
AfterTime(secondsNumber,function,params*)
WaitTime(Time="2020/01/02 03:04:05",function,params*)
;//类
t1:=new MyTimer()
t2:=new MyTimer(durationString, function, params*)
t3:=new MyTimer(seconds, function, params*)
t1.Add(seconds,function,params*)
t1.Add(durationString,function,params*)
t1.DoStart()
t1.DoStop()
;//注意,一次只使用一个计时器,否则鼠标操控会互相冲突的
;//但是通过Add添加的脚本不会冲突
;//也可以不创建类,直接调用AfterTime或者WaitTime创建动作队列
;//此二者创建的队列是共享的同一个静态MyTimer对象,所以不会冲突
;//new MyTimer和AfterTime或WaitTime同时使用也会冲突
mytimer.ahk代码
#include extooltip.ahk
#include extype.ahk
;TimeFormat(s)
;{
;}
SecSub(Big, Small)
{
buffer := Big
EnvSub, buffer, %Small%, Seconds
return buffer
}
SecAdd(Big, secs)
{
buffer := Big
EnvAdd,buffer, %secs%, Seconds
return buffer
}
DurationFormat(s)
{
Y := 0
M := 0
D := 0
H := 0
Mi := 0
Se := 0
Fr := ""
R2 := Mod(s, 60)
R1 := Floor(s / 60)
if(R2 < 10)
{
Fr=0%R2%
}
else
{
Fr=%R2%
}
if(R1==0)
{
return Fr
}
R2 := Mod(R1, 60)
R1 := Floor(R1/60)
if(R2 < 10)
{
Fr=0%R2%"%Fr%
}
else
{
Fr=%R2%"%Fr%
}
if(R1==0)
{
return Fr
}
R2 := Mod(R1, 24)
R1 := Floor(R1/24)
if(R2 < 10)
{
Fr=0%R2%'%Fr%
}
else
{
Fr=%R2%'%Fr%
}
if(R1==0)
{
return Fr
}
Re := R1 . "d" . Fr
return Re
}
DurationParse(s)
{
if(RegExMatch(s,"(\d+)d([01]?\d|2[0-3])'([0-5]?\d)""([0-5]?\d)",m))
{
s := m1 * 24 + m2
s := s * 60 + m3
s := s * 60 + m4
return s
}
if(RegExMatch(s,"([01]?\d|2[0-3])'([0-5]?\d)""([0-5]?\d)",m))
{
s := m1 * 60 + m2
s := s * 60 + m3
return s
}
if(RegExMatch(s,"([0-5]?\d)""([0-5]?\d)",m))
{
s := m1 * 60 + m2
return s
}
if(RegExMatch(s,"""([0-5]?\d)",m))
{
return m1
}
}
class TimerItem
{
__New(duration, fnc)
{
this.duration := duration
this.fnc := fnc
}
}
class MyTimer
{
CurDuration()
{
n := Now()
return SecSub(n, this.StartTime)
}
Add(after, fnc, params*)
{
newfnc := fnc.Bind(params*)
after := this.cur + after
newitem := new TimerItem(after, newfnc)
for index, item in this.Funcs
{
if(item.duration > after)
{
this.Funcs.InsertAt(index, newitem)
return
}
}
this.Funcs.Push(newitem)
}
LastItem()
{
l := this.Funcs.Length()
if(l != 0)
{
return this.Funcs[l]
}
}
DoStart()
{
if(this.busy)
{
return
}
if(this.Funcs.Length() > 0)
{
this.StartTime := Now()
this.cur := 0
timer := this.timer
SetTimer, % timer, 1000
this.busy := true
}
}
DoStop()
{
this.busy := false
timer := this.timer
this.cur := 0
this.Funcs := []
SetTimer, % timer, Off
}
Tick()
{
if(this.Funcs.Length() == 0)
this.DoStop()
for index, item in this.Funcs
{
if(item.duration <= this.cur)
{
item.fnc.Call()
this.cur := secsub(Now(),this.StartTime)
this.Funcs.RemoveAt(index)
return
}
}
if(this.Funcs.Length() == 0)
this.DoStop()
if(this.LastItem().duration < this.cur)
this.DoStop()
this.cur := this.cur + 1
return
}
; __Delete()
; {
; timer := this.timer
; SetTimer, % timer, Off
; this.Funcs := []
; MsgBox end
; }
__New(params*)
{
this.Funcs := []
this.cur := 0
this.StartTime := Now()
this.busy := false
this.timer := ObjBindMethod(this, "Tick")
if(params.Length() >= 2)
{
after := params[0]
fnc := params[1]
params.RemoveAt(0,2)
this.Add(after, fnc, params*)
this.DoStart()
}
}
}
Now()
{
return AfterNow(0)
}
AfterNow(s)
{
if(type(s) == "String")
{
s:=DurationParse(s)
}
Now := ""
EnvAdd, Now, s, Sencond
return Now
}
AfterTime(duration,fnc, params*)
{
static timer := null
if(type(duration) == "String")
{
duration := DurationParse(duration)
}
if(timer == null)
{
timer := new MyTimer()
timer.Add(duration, fnc, params*)
timer.DoStart()
return
}
timer.Add(duration, fnc, params*)
}
WaitTime(time,fnc,params*)
{
time := RegExReplace(time, "\D", "")
secs := Ceil( secsub(time, Now()))
afterTime(secs, fnc, params*)
}