Subversion Repositories ALCASAR

Rev

Go to most recent revision | Details | Last modification | View Log

Rev Author Line No. Line
2788 rexy 1
/*
2
 *
3
 *	jQuery Timer plugin v0.1
4
 *		Matt Schmidt [http://www.mattptr.net]
5
 *
6
 *	Licensed under the MIT License
7
 *
8
 */
9
 
10
 jQuery.timer = function (interval, callback)
11
 {
12
 /**
13
  *
14
  * timer() provides a cleaner way to handle intervals  
15
  *
16
  *	@usage
17
  * $.timer(interval, callback);
18
  *
19
  *
20
  * @example
21
  * $.timer(1000, function (timer) {
22
  * 	alert("hello");
23
  * 	timer.stop();
24
  * });
25
  * @desc Show an alert box after 1 second and stop
26
  * 
27
  * @example
28
  * var second = false;
29
  *	$.timer(1000, function (timer) {
30
  *		if (!second) {
31
  *			alert('First time!');
32
  *			second = true;
33
  *			timer.reset(3000);
34
  *		}
35
  *		else {
36
  *			alert('Second time');
37
  *			timer.stop();
38
  *		}
39
  *	});
40
  * @desc Show an alert box after 1 second and show another after 3 seconds
41
  *
42
  * 
43
  */
44
 
45
	var interval = interval || 100;
46
 
47
	if (!callback)
48
		return false;
49
 
50
	_timer = function (interval, callback) {
51
		this.stop = function () {
52
			clearInterval(self.id);
53
		};
54
 
55
		this.internalCallback = function () {
56
			callback(self);
57
		};
58
 
59
		this.reset = function (val) {
60
			if (self.id)
61
				clearInterval(self.id);
62
 
63
			var val = val || 100;
64
			this.id = setInterval(this.internalCallback, val);
65
		};
66
 
67
		this.interval = interval;
68
		this.id = setInterval(this.internalCallback, this.interval);
69
 
70
		var self = this;
71
	};
72
 
73
	return new _timer(interval, callback);
74
 };