Timer

更新时间:
复制 MD 格式

Cards support delayed execution and repeated execution through built-in timer functions.

setTimeout

Calls a function or executes a code snippet after a specified delay.

The following example executes the arrow function after 1 second.

setTimeout(() => {
  console.info("setTimeout");
}, 1000);

setInterval

Calls a function or executes a code snippet repeatedly at fixed time intervals.

The following example executes the arrow function every 1 second.

setInterval(() => {
  console.info("setTimeout");
}, 1000);

Clear timer

For setTimeout, call clearTimeout before the callback fires to cancel the timer. After the callback has executed, no manual cleanup is needed.

For setInterval, you must call clearInterval to cancel the timer. Otherwise, a memory leak occurs.

Example:

// setTimeout
var timer1 = setTimeout(() => {
  console.info("setTimeout");
}, 1000);

clearTimeout(timer1);

// setInterval 
var timer2 = setInterval(() => {
  console.info("setInterval");
}, 1000);

clearInterval(timer2);

Example code

Download detailTimer.zip for the complete example code.