12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- #If __TARGET__="emscripten"
- #Import "timer_emscripten"
- #Else
- Namespace std.timer
- Private
- Using std.time
- Using std.collections
- Using std.fiber
- Public
- #rem monkeydoc The Timer class.
- #end
- Class Timer
- #rem monkeydoc Creates a new Timer.
-
- Creates new timer with `hertz` frequency.
-
- The timer will continually invoke the `fired` function at the rate of `hertz` times per second.
-
- #end
- Method New( hertz:Double,fired:Void() )
-
- New Fiber( Lambda()
-
- Local period:=1/hertz
-
- Local timeout:=Now()+period
-
- While Not _cancelled
-
- Local now:=Now()
-
- Local sleep:=timeout-now
- If sleep>0
- Fiber.Sleep( sleep )
- Continue
- Endif
-
- If _cancelled Exit
-
- If Not _suspended fired()
- timeout+=period
- Wend
-
- End )
- End
- #rem monkeydoc Timer suspended state.
- #end
-
- Property Suspended:Bool()
-
- Return _suspended
-
- Setter( suspended:Bool )
-
- _suspended=suspended
- End
-
- #rem monkeydoc Cancels the timer.
- #end
- Method Cancel()
-
- _cancelled=True
- End
-
- Private
-
- Field _suspended:Bool
-
- Field _cancelled:Bool
- End
- #Endif
|