Creating a timer using Lua

前端 未结 6 868
粉色の甜心
粉色の甜心 2020-12-31 13:50

I would like to create a timer using Lua, in a way that I could specify a callback function to be triggered after X seconds have passed.

What would be the best way t

6条回答
  •  青春惊慌失措
    2020-12-31 13:57

    If milisecond accuracy is not needed, you could just go for a coroutine solution, which you resume periodically, like at the end of your main loop, Like this:

    require 'socket' -- for having a sleep function ( could also use os.execute(sleep 10))
    timer = function (time)
        local init = os.time()
        local diff=os.difftime(os.time(),init)
        while diff

    This uses the sleep function in LuaSocket, you could use any other of the alternatives suggested on the Lua-users Wiki

提交回复
热议问题