timer.monkey2 754 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #If __TARGET__="emscripten"
  2. #Import "timer_emscripten"
  3. #Else
  4. Namespace std.timer
  5. Private
  6. Using std.time
  7. Using std.collections
  8. Using std.fiber
  9. Public
  10. Class Timer
  11. Method New( hertz:Double,fired:Void() )
  12. New Fiber( Lambda()
  13. Local period:=1/hertz
  14. Local timeout:=Now()+period
  15. While Not _cancelled
  16. Local now:=Now()
  17. Local sleep:=timeout-now
  18. If sleep>0
  19. Fiber.Sleep( sleep )
  20. Continue
  21. Endif
  22. If Not _suspended fired()
  23. timeout+=period
  24. Wend
  25. End )
  26. End
  27. Property Suspended:Bool()
  28. Return _suspended
  29. Setter( suspended:Bool )
  30. _suspended=suspended
  31. End
  32. Method Cancel()
  33. _cancelled=True
  34. End
  35. Private
  36. Field _suspended:Bool
  37. Field _cancelled:Bool
  38. End
  39. #Endif