I found that there are three main functions in the timer mod, set_timer
, set_timer_interval
, clear_timer
. Among them, set_timer
is used to set a one-time task, set_timer_interval
is used to set a periodic task, and clear_timer
is used to cancel a one-time task/periodic task.
- Since
set_timer
is a one-time task, why not automatically delete TimerId after the task is completed? If a large amount of TimerId produced byset_timer
accumulates, will it affect the performance of the canister or the consumption of cycles? for example:
#[update]
fn again() {
set_timer_interval(Duration::from_secs(10), || {
ic_cdk::print("0");
set_timer(Duration::from_secs(1), || {
ic_cdk::print("1");
set_timer(Duration::from_secs(1), || {
ic_cdk::print("2");
set_timer(Duration::from_secs(1), || {
ic_cdk::print("3");
set_timer(Duration::from_secs(1), || {
ic_cdk::print("4");
});
});
});
});
});
}
I need to execute 5 tasks sequentially every 10 seconds. In this way, a large number of TimerId
will be generated after a period of time.
2. Can periodic tasks be terminated by task itself? Currently, periodic tasks set by set_timer_interval
can be terminated only by manually calling clear_timer
. Can periodic tasks be terminated automatically when certain conditions are met in the task?