-
Notifications
You must be signed in to change notification settings - Fork 0
/
timerFlip.js
58 lines (53 loc) · 1.71 KB
/
timerFlip.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
ko.extenders.timerFlip = function (target, params) {
var intervalId = 0,
seconds = 0,
currentSeconds = 0,
loop = null,
currentLoop = 0,
onLoopFn = null,
onLoopFnParams = null;
if(_.isObject(params)){
seconds = params.seconds;
loop = params.loop;
onLoopFn = params.onLoopFn;
onLoopFnParams = params.onLoopFnParams;
}
else
seconds = params;
target.tickerCounter = ko.observable(seconds);
target.tickerRunning = ko.observable(false);
target.tickerOn = function(){
target.tickerRunning(true);
target.tickerCounter(seconds);
currentSeconds = seconds;
currentLoop = loop;
intervalId = window.setInterval(timerTick, seconds * 100);
};
target.tickerOff = function(){
window.clearInterval(intervalId);
target.tickerCounter(0);
target.tickerRunning(false);
};
function timerTick() {
currentSeconds--;
if(currentSeconds !== 0){
target.tickerCounter(currentSeconds);
}
else {
if((loop === 0 || currentLoop !== 0)){
if(loop !== 0)
currentLoop--;
if(_.isFunction(onLoopFn)){
onLoopFn(onLoopFnParams ? onLoopFnParams : null);
}
currentSeconds = seconds;
target.tickerCounter(currentSeconds);
if(loop !== 0 && currentLoop === 0)
target.tickerOff();
}
else
target.tickerOff();
}
}
return target;
};