[AutoHotkey]mytimer.ahk

此类依赖extooltip.ahk,extype.ahk

【2023/6/18】声明:此类已经重构,加入了循环timer,可以简单多线程执行脚本。不过自从遇到火灾抑郁后,写代码积极性大减,慢慢调试吧。

声明:

  1. 所贴出的代码纯属个人原创
  2. 所有代码经过测试才贴,但不保证所有功能最终完成,可能后续会有更新,欢迎随时回顾
  3. 所有非原创的代码,会声明,尽量给出引用出处

主要接口

  1. 主要把时间间隔基础定位基于秒(内置基于毫秒)
  2. 建立一个类,可以自由添加线程到时间队列
  3. 这个时间队列特地设定成顺序执行,确保鼠标操作不会同时执行而引起冲突
  4. 执行完动作后,会重新刷新计时器到正确的计时步骤,确保临近挂起的线程和后续线程能够执行
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*)
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值