Home > SYS_Timer > setIntervalTimer
SYS_Timer.setIntervalTimer() method
Set an interval timer
Signature
typescript
function setIntervalTimer(
id: string,
timeout: number,
callFn: (...args: any) => void,
...args: any
): boolean;1
2
3
4
5
6
2
3
4
5
6
Parameters
Parameter | Type | Description |
|---|---|---|
id | string | Timer ID, used to locate and delete the timer |
timeout | number | Timer duration, unit ms |
callFn | (...args: any) => void | Function called by the timer |
args | any | Arguments passed to the timer callback function |
Returns
boolean
Whether the timer was set successfully
Remarks
If a timer with a duplicate ID is encountered, the previously set timer will be cleared
Example
javascript
// 1. 设置每 200ms 触发一次的循环定时器,统计触发次数
let tickCount = 0;
const created = eda.sys_Timer.setIntervalTimer('嘉立创示例_循环', 200, () => { tickCount++; });
// 2. 让它运行约 1.5 秒
await new Promise(resolve => setTimeout(resolve, 1500));
const ticksAtClear = tickCount;
// 3. 用完清除,定时器停止(净零收尾,可重复运行)
const cleared = eda.sys_Timer.clearIntervalTimer('嘉立创示例_循环');
// 4. 再等一会,确认清除后没有继续触发
await new Promise(resolve => setTimeout(resolve, 600));
console.log('设置结果:', created);
console.log('清除结果:', cleared);
console.log('运行期间触发次数:', ticksAtClear, ',清除后新增:', tickCount - ticksAtClear);1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17