-
Notifications
You must be signed in to change notification settings - Fork 0
/
countdown.js
81 lines (77 loc) · 2.65 KB
/
countdown.js
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
* jQuery plugin for Countdown
*
* @author Jon z <jon@brokendisk.com>
* @copyright 2010-infinity BrokenDisk <http://brokendisk.com>
* @usage: http://brokendisk.com/code/countdown.html
*/
(function($) {
/**
* string target: the id of the element that will contain the countdown.
* object options:
* duration - number of ticks to count, default 30
* interval - length in seconds of each tick, default 1
* function callback: function to call when finished counting down.
*/
function brokenCountdown(target, options, callback) {
this.id = target;
this.duration = options.duration || 30;
this.interval = options.interval || 1;
this.current = this.duration;
this.callback = callback || function() { return true; };
$('#' + this.id).text(this.current);
this.iterate();
}
brokenCountdown.prototype.iterate = function() {
// cancercancercancer
var that = this;
setTimeout(
function() {
$('#' + that.id).text(--that.current);
if (that.current > 0) {
that.iterate();
}
else {
that.callback();
}
},
that.interval * 1000
);
};
/**
* Polymorphic-ish
* Countdown options may be specified by attributes in the html
* or by function arguments;
* in the case of a collision
* the javascript functional arguments will take precedence.
*/
$.fn.brokenCountdown = function(options, callback) {
return this.each(function() {
// countdown([callback])
if (typeof(options) == "function") {
callback = options;
options = undefined;
}
// countdown()
if (typeof(options) == "undefined") {
options = {
duration: $(this).attr('duration'),
interval: $(this).attr('interval')
}
}
// countdown([duration], [callback])
else if (typeof(options) == "number") {
options = {
duration: options,
interval: $(this).attr('interval')
};
}
// countdown([options], [callback])
else if (typeof(options) == "object") {
options.interval = options.interval || $(this).attr('interval');
options.duration = options.duration || $(this).attr('duration');
}
new brokenCountdown(this.id, options, callback);
});
};
})(jQuery);