Timer

Timers on the IC can be set to schedule one-off or periodic tasks using the setTimer and recurringTimer functions respectively.

These functions take a Duration variant that specifies the time in either seconds or nanoseconds. Also, a callback function (the job to be performed) with type () -> async () is passed to the timer functions that will be called when the timer expires.

setTimer and recurringTimer return a TimerId that can be used to cancel the timer before it expires. Timers are canceled with cancelTimer.

The convention is to name the module alias after the file name it is defined in.

import Timer "mo:base/Timer";

On this page

Type Duration
Type TimerId

Function setTimer
Function recurringTimer
Function cancelTimer

Type Duration

type Duration = {#seconds : Nat; #nanoseconds : Nat}

Type TimerId

type TimerId = Nat

Timer.setTimer

func setTimer(d : Duration, job : () -> async ()) : TimerId

Timer.recurringTimer

func recurringTimer(d : Duration, job : () -> async ()) : TimerId

Timer.cancelTimer

func cancelTimer : TimerId -> ()