summaryrefslogtreecommitdiff
path: root/itjs/timer.js
blob: ee04cbbcf76da1fbe5f5220df0fbc8921587b710 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
 * Start new timer
 * Example: timer = new it_timer({ object:this, method:"timer", timeout:100});
 *
 * @param p.object Object to call method in when timer fires
 * @param p.method String with method name to call in object
 * @param p.timeout Timeout in milliseconds
 * @param p.continuous One-shot or continuous timer, default is one-shot
 * @return timer id (deprecated, use method stop() instead)
 */
function it_timer(p)
{
	this.func = p.continuous ? "Interval" : "Timeout";
	return this.timer = window["set" + this.func](function() { p.object[p.method](p) }, p.timeout);
}

it_timer.prototype =
{

stop: function()
{
	if (this.timer)
	{
		window["clear" + this.func](this.timer);
		this.timer = null;
	}
} /* NO COMMA */

}

/**
 * Global helper function to benchmark javascript
 * @parameter label Message label like "start" or "end"
 * @paramter print Whether to output timerlog via ED() and clear it (optional)
 */
function it_timerlog(label, print)
{
	if (window.it_timerlog_active)
	{
		var end = new Date().getTime();

		if (typeof window.it_timernow != "undefined")
		{
			var start = window.it_timernow;

			if (window.it_timerlogmsg != "")
				window.it_timerlogmsg += ", ";

			window.it_timerlogmsg += label + ":" + (end - start);
		}
		else
			window.it_timerlogmsg = "";

		window.it_timernow = end;

		if (print)
		{
			ED("timerlog: " + window.it_timerlogmsg);
			window.it_timerlogmsg = "";
		}
	}
}

it_timerlog("");	// Set start time