WaitTimer

2.1k Downloads

This small library provide an easy way to handle delays or pause in lua.

! All stuff is based on "OnUpdate events" so you can't use any Use-/Cast Item-/Spell stuff !

What it provides: a) delayed calls: start a timer which will execute code after a time period b) repeated loops: a) can also be used to repeatly call the code c) wait: pause a function

v1.6 upgrade note

Function 'Wait' was renamed to 'Delay'. Plz change your code to keep compatible with future versions.

Renamed the base-addon name. So it's possible to use it without explicit linking it - even if you still should do it when releasing stuff based on it.

Example:

Delay / Fire & Forget calls

-- a delayed function call
WaitTimer.Delay(5, function () DEFAULT_CHAT_FRAME:AddMessage("huhu") end)


-- repeated call (single timer)
WaitTimer.Delay(10, function ()
                  DEFAULT_CHAT_FRAME:AddMessage("wake up")
                  return 10
                end,
                 "mytimer") -- NB: "mytimer" must be a unique name
...
WaitTimer.Stop("mytimer")


-- repeated call
id = WaitTimer.Delay(10, function ()
                  DEFAULT_CHAT_FRAME:AddMessage("wake up")
                  return 10
                end)
...
WaitTimer.Stop(id)


-- with user data
function MyTimer(data) DEFAULT_CHAT_FRAME:AddMessage(data) end
WaitTimer.Delay(5, MyTimer, nil, "Hello World")

Pause / Function interrupt

WaitTimer.Call(worker)

function worker()
            DEFAULT_CHAT_FRAME:AddMessage("ready")
            WaitTimer.CallWait(1)
            DEFAULT_CHAT_FRAME:AddMessage("set")
            WaitTimer.CallWait(1)
            DEFAULT_CHAT_FRAME:AddMessage("go")
        end

Setup:

copy theses files to your addon or any subdirectory of it

  • LibStub.lua
  • WaitTimer.lua

Usually people will use a "lib" subdirectory

Add them at the beginning of your .toc file to make sure they get loaded.

Init:

local WaitTimer = LibStub("WaitTimer")

put it at the beginnig of your lua file where you want to use the WaitTimer.

Usage:

timer_id = WaitTimer.Delay(seconds, function, id, data)
  • seconds= how long to wait
  • function= will be called when time is elapsed
    if the function returns a value, this value is used as new wait_time.
  • id= (optional) a fixed timer id If another timer with the same ID exists it will be replaced
  • data= (optional) will be passed to the function call
  • timer_id= id which can be used in the other functions It's equal "id" when it was provided

WaitTimer.Stop(id)
  • stops the timer without calling the function

resttime= WaitTimer.Remaining(id)
  • return remaining seconds or nil
  • also use it to test if a timer is running
WaitTimer.SetTime(id, delay)
  • reset the wait time
  • if <delay> is ommited the function will be triggert on next update
WaitTimer.Call(fct, data)
  • will call "fct(data)"
  • required for using "CallWait"
WaitTimer.CallWait(time)
  • pause function for 'time' seconds
  • ! only usable if the function was called by "WaitTimer.Call" !