PageRenderTime 26ms CodeModel.GetById 14ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/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 */
 25package haxe;
 26
 27class Timer {
 28	#if (neko || php)
 29	#else
 30
 31	private var id : Null<Int>;
 32
 33	#if js
 34	private static var arr = new Array<Timer>();
 35	private var timerId : Int;
 36	#end
 37
 38	public function new( time_ms : Int ){
 39		#if flash9
 40			var me = this;
 41			id = untyped __global__["flash.utils.setInterval"](function() { me.run(); },time_ms);
 42		#elseif flash
 43			var me = this;
 44			id = untyped _global["setInterval"](function() { me.run(); },time_ms);
 45		#elseif js
 46			id = arr.length;
 47			arr[id] = this;
 48			timerId = untyped window.setInterval("haxe.Timer.arr["+id+"].run();",time_ms);
 49		#end
 50	}
 51
 52	public function stop() {
 53		if( id == null )
 54			return;
 55		#if flash9
 56			untyped __global__["flash.utils.clearInterval"](id);
 57		#elseif flash
 58			untyped _global["clearInterval"](id);
 59		#elseif js
 60			untyped window.clearInterval(timerId);
 61			arr[id] = null;
 62			if( id > 100 && id == arr.length - 1 ) {
 63				// compact array
 64				var p = id - 1;
 65				while( p >= 0 && arr[p] == null )
 66					p--;
 67				arr = arr.slice(0,p+1);
 68			}
 69		#end
 70		id = null;
 71	}
 72
 73	public dynamic function run() {
 74	}
 75
 76	public static function delay( f : Void -> Void, time_ms : Int ) {
 77		var t = new haxe.Timer(time_ms);
 78		t.run = function() {
 79			t.stop();
 80			f();
 81		};
 82		return t;
 83	}
 84
 85	#end
 86
 87	/**
 88		Returns a timestamp, in seconds
 89	**/
 90	public static function stamp() : Float {
 91		#if flash
 92			return flash.Lib.getTimer() / 1000;
 93		#elseif neko
 94			return neko.Sys.time();
 95		#elseif php
 96			return php.Sys.time();
 97		#elseif js
 98			return Date.now().getTime() / 1000;
 99		#elseif cpp
100			return untyped __time_stamp();
101		#else
102			return 0;
103		#end
104	}
105
106}