/bin/std/haxe/Timer.hx

http://github.com/Yoomee/clippy · Haxe · 106 lines · 68 code · 10 blank · 28 comment · 8 complexity · cabba04279f271223d29327a85491313 MD5 · raw file

  1. /*
  2. * Copyright (c) 2005, The haXe Project Contributors
  3. * All rights reserved.
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * - Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * - Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
  17. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  20. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  23. * DAMAGE.
  24. */
  25. package haxe;
  26. class Timer {
  27. #if (neko || php)
  28. #else
  29. private var id : Null<Int>;
  30. #if js
  31. private static var arr = new Array<Timer>();
  32. private var timerId : Int;
  33. #end
  34. public function new( time_ms : Int ){
  35. #if flash9
  36. var me = this;
  37. id = untyped __global__["flash.utils.setInterval"](function() { me.run(); },time_ms);
  38. #elseif flash
  39. var me = this;
  40. id = untyped _global["setInterval"](function() { me.run(); },time_ms);
  41. #elseif js
  42. id = arr.length;
  43. arr[id] = this;
  44. timerId = untyped window.setInterval("haxe.Timer.arr["+id+"].run();",time_ms);
  45. #end
  46. }
  47. public function stop() {
  48. if( id == null )
  49. return;
  50. #if flash9
  51. untyped __global__["flash.utils.clearInterval"](id);
  52. #elseif flash
  53. untyped _global["clearInterval"](id);
  54. #elseif js
  55. untyped window.clearInterval(timerId);
  56. arr[id] = null;
  57. if( id > 100 && id == arr.length - 1 ) {
  58. // compact array
  59. var p = id - 1;
  60. while( p >= 0 && arr[p] == null )
  61. p--;
  62. arr = arr.slice(0,p+1);
  63. }
  64. #end
  65. id = null;
  66. }
  67. public dynamic function run() {
  68. }
  69. public static function delay( f : Void -> Void, time_ms : Int ) {
  70. var t = new haxe.Timer(time_ms);
  71. t.run = function() {
  72. t.stop();
  73. f();
  74. };
  75. return t;
  76. }
  77. #end
  78. /**
  79. Returns a timestamp, in seconds
  80. **/
  81. public static function stamp() : Float {
  82. #if flash
  83. return flash.Lib.getTimer() / 1000;
  84. #elseif neko
  85. return neko.Sys.time();
  86. #elseif php
  87. return php.Sys.time();
  88. #elseif js
  89. return Date.now().getTime() / 1000;
  90. #elseif cpp
  91. return untyped __time_stamp();
  92. #else
  93. return 0;
  94. #end
  95. }
  96. }