Skip to content

Commit d8ae5af

Browse files
committed
Experiment with pause on click
1 parent a57e3fa commit d8ae5af

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

examples/timer-with-pause.html

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!DOCTYPE html>
2+
<body>
3+
<head>
4+
<link href="style.css" rel="stylesheet" type="text/css" media="all">
5+
</head>
6+
7+
<body>
8+
<div class='container'>
9+
<h1 class='timer' data-seconds-left=5></h1>
10+
<section class='actions'></section>
11+
</div>
12+
</body>
13+
14+
<script src="../jquery.js"></script>
15+
<script src="../jquery.simple.timer.js"></script>
16+
<script src="timer-with-pause.js"></script>
17+
</body>

examples/timer-with-pause.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
$(function(){
2+
$('.timer').startTimer({
3+
onComplete: function(element){
4+
$('html, body').addClass('bodyTimeoutBackground');
5+
},
6+
allowPause: true // click events will pause/resume timer
7+
})
8+
})

jquery.simple.timer.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
};
4545

4646
this.targetElement.each(function(_index, timerBox) {
47+
var that = this;
4748
var timerBoxElement = $(timerBox);
4849
var cssClassSnapshot = timerBoxElement.attr('class');
4950

@@ -65,6 +66,16 @@
6566
}
6667
});
6768

69+
timerBoxElement.on('pause', function() {
70+
clearInterval(timerBoxElement.intervalId);
71+
timerBoxElement.paused = true;
72+
});
73+
74+
timerBoxElement.on('resume', function() {
75+
timerBoxElement.paused = false;
76+
that.startCountdown(timerBoxElement, { secondsLeft: timerBoxElement.data('timeLeft') });
77+
});
78+
6879
createSubDivs(timerBoxElement);
6980
return this.startCountdown(timerBoxElement, options);
7081
}.bind(this));
@@ -111,8 +122,18 @@
111122
}.bind(this);
112123

113124
element.onComplete = options.onComplete || defaultComplete;
125+
element.allowPause = options.allowPause || false;
126+
if(element.allowPause){
127+
element.on('click', function() {
128+
if(element.paused){
129+
element.trigger('resume');
130+
}else{
131+
element.trigger('pause');
132+
}
133+
});
134+
}
114135

115-
var secondsLeft = this.fetchSecondsLeft(element);
136+
var secondsLeft = options.secondsLeft || this.fetchSecondsLeft(element);
116137

117138
var refreshRate = options.refreshRate || 1000;
118139
var endTime = secondsLeft + this.currentTime();
@@ -122,6 +143,7 @@
122143

123144
intervalId = setInterval((function() {
124145
timeLeft = endTime - this.currentTime();
146+
element.data('timeLeft', timeLeft);
125147
this.setFinalValue(this.formatTimeLeft(timeLeft), element);
126148
}.bind(this)), refreshRate);
127149

0 commit comments

Comments
 (0)