-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtimer.lua
36 lines (33 loc) · 957 Bytes
/
timer.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
--------------------------------------------------
-- Some timer nessesary for live spellchecking
--------------------------------------------------
local _M = {}
function _M.create_timer(on_expire, timeout)
-- Creates new timer object with given timeout and calling 'on_expire' when expires
return {
trigger = on_expire,
time = timeout,
last_cycle = false,
running = false
}
end
function _M.start(timer)
-- Starts timer.
-- If timer already started - gives additional timeout equivalent to original.
-- Full duration of additional timeouts and original wont be longer whan two original timeouts.
timer.last_cycle = false
if not timer.running then
timer.running = true
timeout(timer.time, function(t)
if t.last_cycle then
t.trigger()
t.running = false
t.last_cycle = false
return false
end
t.last_cycle = true
return true
end, timer)
end
end
return _M