Files
DP-S-Script/Dps_A/BaseClass/TimerClass/TimerClass.nut
lenheart 7f547f5fd4 ### 新增IO 类
### 新增MD5 类

### 新增Mysql 类

### 新增Timer 类
2024-09-20 19:24:32 +08:00

58 lines
1.5 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
文件名:TimerClass.nut
路径:Dps_A/BaseClass/TimerClass/TimerClass.nut
创建日期:2024-09-19 12:39
文件用途:定时器类
*/
class Timer {
//执行任务队列树
Wait_Exec_Tree = null;
constructor() {
Wait_Exec_Tree = RedBlackTree();
Cb_timer_dispatch_Func.rawset("__System__Timer__Event", Update.bindenv(this));
}
function Update() {
local Node = Wait_Exec_Tree.pop();
if (!Node) {
return;
}
//取得函数体
local Info = Node.Info;
//执行时间
local exec_time = Node.time;
//如果没到执行时间,放回去,等待下次扫描
if (time() <= exec_time) {
Wait_Exec_Tree.insert(exec_time, Node.Info);
return;
}
//函数
local func = Info[0];
//参数
local func_args = Info[1];
//执行函数
func.acall(func_args);
}
function SetTimeOut(target_func, delay_time, ...) {
local target_arg_list = [];
target_arg_list.push(getroottable());
for (local i = 0; i< vargv.len(); i++) {
target_arg_list.push(vargv[i]);
}
//当前时间戳单位s
local time_sec = time();
//计算下一次执行的时间
local exec_time_sec = time_sec + (delay_time - 1);
//设置下一次执行
local func_info = [];
func_info.push(target_func);
func_info.push(target_arg_list);
_Timer_Object.Wait_Exec_Tree.insert(exec_time_sec, func_info);
}
}