dp-s_doc/Object/Timer/Timer.md

110 lines
2.8 KiB
Markdown
Raw Permalink 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.

# Timer 类函数文档
**类说明**
`Timer`类为定时器相关操作。
---
### 函数签名
**公有函数**
- `function SetTimeOut(target_func, delay_time, ...)`
### 参数
- `target_func`:函数体,要获执行的函数
- `delay_time`:整数,要延迟的时间(秒)
- `...`:可变参数,传入的参数可再函数体回调函数中使用
**这里给出两个个例子**
> 无参数
```
Timer.SetTimeOut(function() {
print("注册 5 秒后执行")
}, 5);
```
---
> 有参数
```
Timer.SetTimeOut(function(str,num) {
print(str);//将打印 "字符串"
print(num);//将打印 123
print("注册 5 秒后执行")
}, 5,"字符串",123);
```
---
---
### 函数签名
**公有函数**
- `function SetCronTask(target_func, build_info, ...)`
### 参数
- `target_func`:函数体,要获执行的函数
- `build_info`:可以是字符串,计划任务格式 让gpt帮你写cron字符串 也可以是一个结构体 包含 Cron 和 Name
- `...`:可变参数,传入的参数可再函数体回调函数中使用
### 注意
- `关于定时任务的销毁`:在执行的任务中 如果不返回 或者返回null 返回true 任务都将继续执行 如果返回false 任务将不再继续执行
**这里给出三个例子**
> 无参数
```
Timer.SetCronTask(function() {
print("注册 每五秒 执行")
}, "*/5 * * * * ?");
```
---
> 有参数
```
Timer.SetCronTask(function(str,num) {
print(str);//将打印 "字符串"
print(num);//将打印 123
print("注册 每五秒 执行")
}, "*/5 * * * * ?","字符串",123);
```
> 结构体
```
Timer.SetCronTask(function(str,num) {
print(str);//将打印 "字符串"
print(num);//将打印 123
print("注册 每五秒 执行")
}, {
Cron = "0/5 * * * * *",
Name = "TestCronName"
},"字符串",123);
```
---
### 函数签名
**公有函数**
- `function RemoveCronTask(name)`
### 参数
- `name`:要移除的定时任务的名字
**这里给出一个例子**
```
//定义一个显示的变量
getroottable().TaskCount <- 0;
//任务
function Task() {
getroottable().TaskCount++;
print("定时任务执行了一次,当前执行次数为:" + getroottable().TaskCount);
}
//注册一个定时任务 每五秒执行一次 任务名为 TestCronName
Timer.SetCronTask(Task, {
Cron = "0/5 * * * * *",
Name = "TestCronName"
});
//十秒钟后删除之前注册的定时任务
Timer.SetTimeOut(function() {
print("删除定时任务");
//这里执行删除定时任务的逻辑以后 上面的定时任务将不会在继续生效
Timer.RemoveCronTask("TestCronName");
}, 10000)
```
---