diff options
| author | Yorkie Liu <yorkiefixer@gmail.com> | 2018-05-31 23:28:14 +0800 |
|---|---|---|
| committer | Ryan Dahl <ry@tinyclouds.org> | 2018-05-31 23:39:44 +0200 |
| commit | 502662476ab1f9812ff44e5fd9cd9a1a973a2119 (patch) | |
| tree | 62052157a23937aa7f6742999356ce554594310a | |
| parent | 942daac1da55775c748004a77451e319c00d5c5d (diff) | |
optimize the timer ts impl, and correct the duration if less than 10
| -rw-r--r-- | timers.go | 4 | ||||
| -rw-r--r-- | timers.ts | 37 |
2 files changed, 21 insertions, 20 deletions
@@ -31,6 +31,10 @@ func InitTimers() { Duration: msg.TimerStartDuration, Cleared: false, } + // If this parameter is less than 10, a value of 10 is used + if t.Duration < 10 { + t.Duration = 10 + } t.StartTimer() timers[id] = t return nil @@ -39,15 +39,16 @@ function onMessage(payload: Uint8Array) { } } -export function setTimeout( +function setTimer( cb: TimerCallback, duration: number, + interval: boolean, // tslint:disable-next-line:no-any - ...args: any[] + args: any[] ): number { const timer = { id: nextTimerId++, - interval: false, + interval, duration, args, cb @@ -56,34 +57,28 @@ export function setTimeout( dispatch.sendMsg("timers", { command: pb.Msg.Command.TIMER_START, timerStartId: timer.id, - timerStartInterval: false, + timerStartInterval: interval, timerStartDuration: duration }); return timer.id; } -// TODO DRY with setTimeout +export function setTimeout( + cb: TimerCallback, + duration: number, + // tslint:disable-next-line:no-any + ...args: any[] +): number { + return setTimer(cb, duration, false, args); +} + export function setInterval( cb: TimerCallback, repeat: number, // tslint:disable-next-line:no-any ...args: any[] ): number { - const timer = { - id: nextTimerId++, - interval: true, - duration: repeat, - args, - cb - }; - timers.set(timer.id, timer); - dispatch.sendMsg("timers", { - command: pb.Msg.Command.TIMER_START, - timerStartId: timer.id, - timerStartInterval: true, - timerStartDuration: repeat - }); - return timer.id; + return setTimer(cb, duration, true, args); } export function clearTimer(id: number) { @@ -92,3 +87,5 @@ export function clearTimer(id: number) { timerClearId: id }); } + + |
