msgProc: Timers

Timers provide recurring execution at specified intervals. Use them for clocks, countdowns, auto-save, polling, and game loops. Create a timer, handle MSG_TIMER events, kill when done - exactly like Win32's SetTimer/KillTimer API.

How Timers Work

The timer system provides a simple workflow for scheduling recurring tasks:

  1. Create a timer with wakaPAC.setTimer(pacId, interval)
  2. Store the returned timer ID to identify this specific timer
  3. Handle MSG_TIMER events in msgProc, checking event.wParam to identify which timer fired
  4. Kill the timer with wakaPAC.killTimer(pacId, timerId) when no longer needed
  5. Timers automatically clean up when the component is destroyed
Multiple Timers: You can create multiple timers per component. Each timer gets a unique ID, and you check event.wParam in MSG_TIMER handlers to determine which timer fired.

Timer Elapsed (MSG_TIMER)

MSG_TIMER fires when a timer created with wakaPAC.setTimer() elapses. Use timers for clocks, countdowns, periodic refreshes, polling, and game ticks. Timers provide reliable recurring execution at specified intervals.

Message Parameters

Parameter Type Description
wParam number Timer ID that elapsed (the value returned from setTimer())
lParam number Reserved (always 0)

Example: Clock

init() {
    this.clockTimer = wakaPAC.setTimer(this.pacId, 1000);
}

msgProc(event) {
    if (event.message === wakaPAC.MSG_TIMER && event.wParam === this.clockTimer) {
        this.time = new Date().toLocaleTimeString();
    }
}

destroy() {
    wakaPAC.killTimer(this.pacId, this.clockTimer);
}

Example: Countdown Timer

init() {
    this.remaining = 60; // 60 seconds
    this.countdownTimer = wakaPAC.setTimer(this.pacId, 1000);
}

msgProc(event) {
    if (event.message === wakaPAC.MSG_TIMER && event.wParam === this.countdownTimer) {
        this.remaining--;

        if (this.remaining <= 0) {
            wakaPAC.killTimer(this.pacId, this.countdownTimer);
            this.onTimerComplete();
        }
    }
}

Timer API Reference

setTimer(pacId, elapse)

const timerId = wakaPAC.setTimer(this.pacId, 1000);

Creates a timer that fires every elapse milliseconds. Returns an auto-generated timer ID that you use to identify the timer in MSG_TIMER events and to kill the timer later.

Parameter Type Description
pacId string Component ID to receive MSG_TIMER events
elapse number Interval in milliseconds between timer events
Returns Type Description
timerId number Auto-generated timer ID to identify this timer

killTimer(pacId, timerId)

wakaPAC.killTimer(this.pacId, this.clockTimer);

Stops a specific timer. The timer stops firing MSG_TIMER events immediately.

Parameter Type Description
pacId string Component ID that owns the timer
timerId number Timer ID to stop (returned from setTimer)
Returns Type Description
success boolean True if timer was found and killed, false otherwise

killAllTimers(pacId)

wakaPAC.killAllTimers(this.pacId);

Stops all timers for a component. Useful for cleanup when a component is being destroyed or when you need to reset all timer state.

Parameter Type Description
pacId string Component ID to stop all timers for
Returns Type Description
count number Number of timers that were killed

Timer Lifecycle and Cleanup

Timers automatically clean up when the component is destroyed, but explicit management is recommended for clarity and to avoid unnecessary processing.

Example: Manual Timer Management

init() {
    this.timer = wakaPAC.setTimer(this.pacId, 5000);
}

stop() {
    wakaPAC.killTimer(this.pacId, this.timer);
    this.timer = null;
}

restart() {
    this.stop();
    this.timer = wakaPAC.setTimer(this.pacId, 5000);
}

destroy() {
    // Optional - timers auto-cleanup on destroy
    // But explicit cleanup is good practice
    wakaPAC.killAllTimers(this.pacId);
}

Best Practices

  • Check timer ID in msgProc: When using multiple timers, check event.wParam to identify which timer fired
  • Store timer IDs: Save the returned timer ID to kill specific timers later
  • Avoid rapid timers: Very short intervals (<16ms) impact performance.
  • Clean up timers: Kill timers when they're no longer needed to prevent unnecessary processing
  • Battery awareness: Long-running timers on mobile drain battery. Consider pausing timers when app is backgrounded