-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnever-ending-progress.js
69 lines (56 loc) · 2.18 KB
/
never-ending-progress.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
(function($) {
function NeverEndingProgress(opts) {
this.update_progress = function(progress) {
this.current_progress = this.max_progress * progress;
if (progress == 1) {
this.start_auto_progress();
};
this.callback_progress();
};
this.finish = function() {
this.current_progress = 1;
this.callback_progress();
};
this.start_auto_progress = function() {
this.auto_progress_time = new Date().getTime();
var init_to_auto_time = this.auto_progress_time - this.start_time;
this.expected_time = (init_to_auto_time / this.max_progress) * this.auto_progress_required;
this.update_interval = (this.expected_time / 100) / this.auto_progress_required;
this.auto_progress = 0;
this.auto_interval();
};
this.auto_interval = function() {
var self = this;
this.auto_progress += 0.01;
this.current_progress += 0.01;
var interval_slowdown = (1 / Math.pow(0.99 - this.current_progress, 3));
this.callback_progress();
if (this.current_progress < 0.99) {
this.interval = setTimeout(function(){
self.auto_interval();
}, this.update_interval + interval_slowdown);
};
}
this.callback_progress = function() {
if (typeof(this.progress_callback) == 'function') {
this.progress_callback(this.current_progress);
};
};
this.init = function() {
this.current_progress = 0;
this.max_progress = 0.7;
$.extend(this, opts);
this.auto_progress_required = (1 - this.max_progress);
this.start_time = new Date().getTime();
};
this.init();
}
$.fn.never_ending_progress = function(max_progress, progress_callback) {
var $this = $(this);
$this.never_ending_progress = new NeverEndingProgress({
max_progress: max_progress,
progress_callback: progress_callback
});
return $this;
}
})(jQuery);