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:
- Create a timer with
wakaPAC.setTimer(pacId, interval) - Store the returned timer ID to identify this specific timer
- Handle
MSG_TIMERevents in msgProc, checkingevent.wParamto identify which timer fired - Kill the timer with
wakaPAC.killTimer(pacId, timerId)when no longer needed - Timers automatically clean up when the component is destroyed
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.wParamto 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