-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.countdown.js
169 lines (145 loc) · 6.26 KB
/
jquery.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
Package: timer
Reusable timer plugin (jquery ui widget based)
*/
;(function( $ )
{
/*
Function: ees.timer
Used to create an image based checkbox replacement.
Parameters:
utc - Provided dates are UTC (defaults to false)
startdate - The starting date for the timer. Either a string
disabled - URL to disabled state image
Returns:
None
*/
$.widget( "ees.jCountdown",
{
options:
{
transitionDelay: 200
, syncLimit: 600 // 10 minutes till resync
, syncCount: 0
, fragment: "countdown.html"
, enddate: {}
, startdate: {}
, remaining: 0
, total: 0
, secondValue: 1000
, minuteValue: 1000 * 60
, hourValue: 1000 * 60 * 60
, dayValue: 1000 * 60 * 60 * 24
}
, _create: function( params )
{
var $widget = this;
var $item = $widget.element;
this.options = $.extend( this.options, params );
var options = this.options;
// Calculate remaining
//
options.startdate.date = new Date( options.startdate[ "year" ],
parseInt( options.startdate[ "month" ] ) - 1,
options.startdate[ "day" ],
options.startdate[ "hours" ] || 0,
options.startdate[ "minutes" ] || 0,
options.startdate[ "seconds" ] || 0,
options.startdate[ "milliseconds" ] || 0 );
options.enddate.date = new Date( options.enddate[ "year" ],
parseInt( options.enddate[ "month" ] ) - 1,
options.enddate[ "day" ],
options.enddate[ "hours" ] || 0,
options.enddate[ "minutes" ] || 0,
options.enddate[ "seconds" ] || 0,
options.enddate[ "milliseconds" ] || 0 );
options.total = options.enddate.date.getTime() - options.startdate.date.getTime();
$widget._sync();
// Load the timer HTML fragment and (start) updating it
//
$item.load( options.fragment, function()
{
// Set the startdate and enddate
//
$item.find( ".startdate .value" ).text( options.startdate.date.toLocaleString() );
$item.find( ".enddate .value" ).text( options.enddate.date.toLocaleString() );
$widget._update();
} );
}
, _sync: function()
{
var $widget = this;
var $item = $widget.element;
var options = $widget.options;
var now = new Date();
options.remaining = options.enddate.date.getTime() - now.getTime();
}
, _update: function()
{
var $widget = this;
var $item = $widget.element;
var options = $widget.options;
// Set day, hour, minutes and seconds value
//
var value = options.remaining;
var days = Math.floor( value / options.dayValue );
if ( days > 0 )
{
value -= days * options.dayValue;
var hours = Math.floor( value / options.hourValue );
value -= hours * options.hourValue;
var minutes = Math.floor( value / options.minuteValue );
value -= minutes * options.minuteValue;
var seconds = Math.floor( value / options.secondValue );
}
else
{
days = 0;
var hours = 0
var minutes = 0;
var seconds = 0;
}
$widget._transition( $item.find( ".days .value" ), days );
$widget._transition( $item.find( ".hours .value" ), hours );
$widget._transition( $item.find( ".minutes .value" ), minutes );
$widget._transition( $item.find( ".seconds .value" ), seconds );
var percentage = 100 - Math.floor( ( options.remaining * 100 ) / options.total );
percentage < 0 ? percentage = 0 : percentage = Math.min( 100, percentage );
$widget._transition( $item.find( ".progress .text" ), percentage + "%" );
$item.find( ".progress .value" ).animate( { width: percentage + "%" }, 10 * percentage )
// (Re)start timer
//
if ( options.remaining > 0 )
{
window.setTimeout( function()
{
options.syncCount++;
if ( options.syncCount > options.syncLimit )
{
$widget._sync();
$widget._update();
options.syncCount = 0;
}
else
{
options.remaining -= 1000;
$widget._update();
}
}, 1000 );
}
}
, _transition: function( $element, newValue )
{
var $widget = this;
var options = $widget.options;
if ( $element.text() != newValue )
{
$element.fadeOut( options.transitionDelay, function()
{
$element.text( newValue );
$element.fadeIn( options.transitionDelay );
} );
}
}
} );
} )( jQuery );