GUIDES Callbacks Suspend Callback
About
Binary Data
Index
Convert String to ArrayBuffer
Convert ArrayBuffer to String
Convert ArrayBuffers to String
Handle Errors Converting ArrayBuffer to String
Immutable ArrayBuffers
Resize an ArrayBuffer
Combine ArrayBuffers
Convert Base64 to Binary Data
Convert Binary Data to Base64
Convert Binary Data to Hex
Convert Hex to Binary Data
Calculate CRC for Binary Data
Compress Binary Data – One Buffer
Compress Binary Data – Streaming
Decompress Binary Data – One Buffer
Decompress Binary Data – Streaming
Callbacks
Index
One-Time Callback
Repeating Callback
Repeating Callback with Initial Delay
Immediate Callback
Reschedule Callback
Cancel Callback
Suspend Callback
What About setTimeout?
Optimizing Embedded JavaScript
Index
When to Optimize
Know Where to Optimize
Looping through an Array
Iterating Over a String
Building a String
Avoid Copying Buffers
Accessing Properties
Map versus Object
Appending to an Array
Operating on Bits
Defining Class Methods
Reducing Stack Use
Time
Index
Get Unix Time
Get Time of Day
Get Date
Get Time Since System Start
Get Microseconds
Set System Date and Time
Set Real-Time Clock Time
Get Time and Date from Network
Get Time and Date from Real-Time Clock
Sleep

Suspend Callback

Use Timer.schedule() to suspend a callback. Once a callback has been suspended, it will not be invoked again until it is rescheduled.

Suspending a callback and later rescheduling is often lighter than creating and canceling callbacks.

This example creates a repeating callback that suspends itself when invoked. Two seconds after being suspended, another callback reschedules the repeating callback to be invoked again in 1000 milliseconds.

const repeating = Timer.repeat(() => {
	trace("tick\n");
	// suspend
	Timer.schedule(repeating);
}, 5000);

Timer.set(() => {
	Timer.schedule(repeating, 1000, 1000);
}, 7000);

Sometimes you want a callback to begin in an unscheduled state, so it can be scheduled later. Both Timer.set() and Timer.repeat() create scheduled callbacks. Use either and then immediately unschedule the callback.

const id = Timer.repeat(() => {
	trace("tick\n");
}, 1000);

// immediately suspend
Timer.schedule(id);