local callback_info = { callback = {}, callback_count = 0 };
local callback_switch = false;
local cookie = 1;
local timer_cache = {};
setmetatable(timer_cache, { __mode = "k" });
local function timer_callback(evid, para)
local callback, arg, is_repeat = para["callback"], para["arg"], para["is_repeat"];
if type(callback) == "function" then
if _DEBUG or callback_switch then
callback_info["callback"] = debug.getinfo(callback, 'S');
end
xpcall(callback, error_handle, arg);
end
end
function timer_event_dispatch(cookie)
cookie = tonumber(cookie)
local timer_info = timer_cache[cookie];
if not timer_info then
return;
end
timer_callback(cookie, timer_info);
end
function get_timer_cache()
return timer_cache;
end
function get_callback_info()
return callback_info["callback"]["short_src"], callback_info["callback"]["linedefined"], callback_info["callback_count"];
end
function set_timer(timeout, callback, arg, is_repeat)
assert(timeout > 0, "超时时间必须>0\n");
local id = timer_event_set(timeout, is_repeat or false);
if not id then
assert(false, "设置定时器失败。\n");
end
local cache_arg;
cache_arg = {
timeout = timeout,
arg = arg,
id = id,
callback = callback,
is_repeat = is_repeat,
};
assert(not timer_cache[id]);
timer_cache[id] = cache_arg;
return id;
end
function delete_timer(time_id)
timer_cache[time_id] = nil;
timer_event_del(time_id);
end
function get_timer(time_id)
return timer_cache[time_id];
end
function get_all_timer()
return timer_cache;
end
function get_timer_count()
return #timer_cache;
end
function callback_switch_on(switch)
callback_switch = switch;
end