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 to achieve this? ( I need to download some data from a webserver that will be parsed once or twice an hour )
Cheers.
If it's acceptable for you, you can try LuaNode. The following code sets a timer:
use Script.SetTimer(interval, callbackFunction)
Try
lalarm
, here: http://www.tecgraf.puc-rio.br/~lhf/ftp/lua/Example (based on src/test.lua):
Output:
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:
This uses the sleep function in LuaSocket, you could use any other of the alternatives suggested on the Lua-users Wiki
After reading this thread and others I decided to go with Luv lib. Here is my solution: