-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.progressTimer.js
60 lines (47 loc) · 2.15 KB
/
jquery.progressTimer.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
var interval;
(function ($) {
$.fn.progressTimer = function (options) {
var settings = $.extend({}, $.fn.progressTimer.defaults, options);
if (settings.timeLimit == -100){
window.clearInterval(interval);
return;
}
this.each(function () {
$(this).empty();
var barContainer = $("<div>").addClass("progress active progress-striped");
var bar = $("<div>").addClass("progress-bar").addClass(settings.baseStyle)
.attr("role", "progressbar")
.attr("aria-valuenow", "0")
.attr("aria-valuemin", "0")
.attr("aria-valuemax", settings.timeLimit);
bar.appendTo(barContainer);
barContainer.appendTo($(this));
var start = new Date();
var limit = settings.timeLimit * 1000;
interval = window.setInterval(function () {
var elapsed = new Date() - start;
bar.width(((elapsed / limit) * 100) + "%");
if (limit - elapsed <= 5000)
bar.removeClass(settings.baseStyle)
.removeClass(settings.completeStyle)
.addClass(settings.warningStyle);
if (elapsed >= limit) {
window.clearInterval(interval);
bar.removeClass(settings.baseStyle)
.removeClass(settings.warningStyle)
.addClass(settings.completeStyle);
settings.onFinish.call(this);
}
}, 250);
});
return this;
};
$.fn.progressTimer.defaults = {
timeLimit: 60, //total number of seconds
warningThreshold: 5, //seconds remaining triggering switch to warning color
onFinish: function () {}, //invoked once the timer expires
baseStyle: '', //bootstrap progress bar style at the beginning of the timer
warningStyle: 'progress-bar-danger', //bootstrap progress bar style in the warning phase
completeStyle: 'progress-bar-success' //bootstrap progress bar style at completion of timer
};
}(jQuery));