diff options
author | 2016-02-14 08:16:35 +0100 | |
---|---|---|
committer | 2016-02-14 08:16:35 +0100 | |
commit | 2db49088141b6238e92aecc4c073076a02c73065 (patch) | |
tree | dba4ea354a0dec2d986fd49de943e5f30b81f8a1 /3rdparty/luv/examples/timers.lua | |
parent | 80e8fe80e6354acc7157bd82066e719365642a1b (diff) |
Added lua-zlib, lfs and luv support for LUA, exposed all using luaengine (nw)
Diffstat (limited to '3rdparty/luv/examples/timers.lua')
-rw-r--r-- | 3rdparty/luv/examples/timers.lua | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/3rdparty/luv/examples/timers.lua b/3rdparty/luv/examples/timers.lua new file mode 100644 index 00000000000..049235e6fb1 --- /dev/null +++ b/3rdparty/luv/examples/timers.lua @@ -0,0 +1,68 @@ +local p = require('lib/utils').prettyPrint +local uv = require('luv') + +local function set_timeout(timeout, callback) + local timer = uv.new_timer() + local function ontimeout() + p("ontimeout", timer) + uv.timer_stop(timer) + uv.close(timer) + callback(timer) + end + uv.timer_start(timer, timeout, 0, ontimeout) + return timer +end + +local function clear_timeout(timer) + uv.timer_stop(timer) + uv.close(timer) +end + +local function set_interval(interval, callback) + local timer = uv.new_timer() + local function ontimeout() + p("interval", timer) + callback(timer) + end + uv.timer_start(timer, interval, interval, ontimeout) + return timer +end + +local clear_interval = clear_timeout + +local i = set_interval(300, function() + print("interval...") +end) + +set_timeout(1000, function() + clear_interval(i) +end) + + +local handle = uv.new_timer() +local delay = 1024 +local function ontimeout() + p("tick", delay) + delay = delay / 2 + if delay >= 1 then + uv.timer_set_repeat(handle, delay) + uv.timer_again(handle) + else + uv.timer_stop(handle) + uv.close(handle) + p("done") + end +end +uv.timer_start(handle, delay, 0, ontimeout) + + +repeat + print("\ntick.") +until uv.run('once') == 0 + +print("done") + +uv.walk(uv.close) +uv.run() +uv.loop_close() + |