lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Tuesday 06 September 2005 11:15 pm, Javier Guerra wrote:
> > Out of curiosity, how do you plan to handle the "every now and then"?
> > Are you thinking of some sort of timer?

this is what we agreed on, but never tried:

---------------------------------------
local runnable_t = {}
function runnable_t:events ()
    return function ()
        if os.time >= myjobs[1].time then
            return table.remove (myjobs, 1)
        else
            return nil
        end
    end
end
function runnable_t:tick (job)
    if type (job) == "table" and type (job.func) == "function" then
        job.func ()
    end
end
copas.addtask (runnable_t)

-- adds a job to be executed
--        f:  function to be called
--        t:  time at which it should be called
function addjob (f,t)
    local job={time=t, func=f}
    local ins = -1
    
    for i,v in ipairs (myjobs) do
        if v.time > t then
            ins = i
            break
        end
    end
    
    if ins > 0 then
        table.insert (myjobs, ins, job)
    else
        table.insert (myjobs, job)
    end
end
--------------------------------------

-- 
Javier

Attachment: pgpr4dLoxGRJY.pgp
Description: PGP signature