Skip to main content

Timer

Timer is a special API component in ElenixOS for creating and managing timers. This document will introduce the features, usage methods, and precautions of the timer component.

Feature Introduction

The timer component allows developers to create and manage timers for executing timed tasks, animations, periodic operations, etc. Timer components are typically used in scenarios that require timed execution, such as countdowns, polling, animations, etc.

Usage Methods

Create Timer

Use the new lv.timer() constructor to create a timer object:

// Create timer object
const timer = new lv.timer();

Configure Timer

Use the following methods to configure the timer:

// Set timer callback function
timer.setCb(() => {
eos.console.log("Timer callback executed");
});

// Set timer period (milliseconds)
timer.setPeriod(1000); // 1 second

// Set timer repeat count
timer.setRepeatCount(5); // Repeat 5 times

// Set timer priority
timer.setPriority(lv.TIMER_PRIO.MID);

// Set timer auto reset
timer.setAutoReset(true);

Start and Stop Timer

Use the following methods to start and stop the timer:

// Start timer
timer.start();

// Stop timer
timer.stop();

// Reset timer
timer.reset();

// Check if timer is running
const isRunning = timer.isRunning();

Example: Create a Simple Timer

// Create timer
const timer = new lv.timer();

// Set timer callback
let count = 0;
timer.setCb(() => {
eos.console.log("Timer tick:", count);
count++;

// Stop timer
if (count >= 5) {
timer.stop();
eos.console.log("Timer stopped");
}
});

// Set timer period
timer.setPeriod(1000); // 1 second

// Start timer
timer.start();

Example: Create a Periodic Timer

// Create timer
const timer = new lv.timer();

// Set timer callback
let count = 0;
timer.setCb(() => {
eos.console.log("Timer tick:", count);
count++;
});

// Set timer period
timer.setPeriod(1000); // 1 second

// Set auto reset
timer.setAutoReset(true);

// Start timer
timer.start();

// Stop timer after 5 seconds
setTimeout(() => {
timer.stop();
eos.console.log("Timer stopped");
}, 5000);

Timer Priority

PriorityDescription
lv.TIMER_PRIO.LOWESTLowest priority
lv.TIMER_PRIO.LOWLow priority
lv.TIMER_PRIO.MIDMedium priority
lv.TIMER_PRIO.HIGHHigh priority
lv.TIMER_PRIO.HIGHESTHighest priority