Skip to content

Allow pause on click #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ $ bower install jquery-simple-timer

The plugin will be installed under *bower_components/jquery-simple-timer/jquery.simple.timer.js* unless you have a *.bowerrc* stating otherwise.

## Live Examples

Open [examples/index.html](https://rawgit.com/caike/jQuery-Simple-Timer/master/examples/index.html)

## Tests

Expand Down
112 changes: 112 additions & 0 deletions examples/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<script src="./../jquery.js"></script>
<script src="./../jquery.simple.timer.js"></script>

<h3>Examples</h3>

<ul>
<li>
<p><strong>Default behavior</strong></div></p>
<p>
<pre><code>// HTML
&lt;div class="timer-quick" data-seconds-left="5"&gt;&lt;/div&gt;

// JavaScript
$(function(){
$('.timer-quick').startTimer();
})

// CSS
.timeout {
color: red;
}
</code></pre>
</p>
<p>
<small>(defaults behavior adds <i>.timeout</i> class when complete)</small>
<div class="timer-quick" data-seconds-left="5"></div>
</p>
</li>

<li>
<p><strong>console.log when complete</strong></div>
<p>
<pre><code>//HTML
&lt;div class="timer" data-seconds-left="25"&gt;&lt;/div&gt;

// JavaScript
$(function(){
$('.timer').startTimer({
onComplete: function(){
console.log('Complete');
}
});
})
</code></pre>
</p>
<p><div class="timer" data-seconds-left="25"></div></p>
</li>

<li>
<p><strong>console.log when complete and allow pause on click</strong></p>
<p><pre><code>// HTML
&lt;div class="timer-pause" data-minutes-left="1.5"&gt;&lt;/div&gt;

// JavaScript
$(function(){
$('.timer').startTimer({
onComplete: function(){
console.log('Complete');
},
allowPause: true
});
})
</code></pre>
<p>
<small>(click on timer to pause/resume)</small>
<div class="timer-pause" data-minutes-left="1.5"></div>
</p>
</li>
</ul>
<style>
.days {
float: left;
margin-right: 4px;
}
.hours {
float: left;
}
.minutes {
float: left;
}
.seconds {
float: left;
}
.clearDiv {
clear: both;
}

.timeout {
color: red;
}

</style>

<script>
$(function(){

$('.timer-quick').startTimer();

$('.timer').startTimer({
onComplete: function(){
console.log('Complete');
}
});

$('.timer-pause').startTimer({
onComplete: function(){
console.log('Complete');
},
allowPause: true
});
})
</script>
39 changes: 0 additions & 39 deletions index.html

This file was deleted.

24 changes: 23 additions & 1 deletion jquery.simple.timer.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
};

this.targetElement.each(function(_index, timerBox) {
var that = this;
var timerBoxElement = $(timerBox);
var cssClassSnapshot = timerBoxElement.attr('class');

Expand All @@ -65,6 +66,16 @@
}
});

timerBoxElement.on('pause', function() {
clearInterval(timerBoxElement.intervalId);
timerBoxElement.paused = true;
});

timerBoxElement.on('resume', function() {
timerBoxElement.paused = false;
that.startCountdown(timerBoxElement, { secondsLeft: timerBoxElement.data('timeLeft') });
});

createSubDivs(timerBoxElement);
return this.startCountdown(timerBoxElement, options);
}.bind(this));
Expand Down Expand Up @@ -111,8 +122,18 @@
}.bind(this);

element.onComplete = options.onComplete || defaultComplete;
element.allowPause = options.allowPause || false;
if(element.allowPause){
element.on('click', function() {
if(element.paused){
element.trigger('resume');
}else{
element.trigger('pause');
}
});
}

var secondsLeft = this.fetchSecondsLeft(element);
var secondsLeft = options.secondsLeft || this.fetchSecondsLeft(element);

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

intervalId = setInterval((function() {
timeLeft = endTime - this.currentTime();
element.data('timeLeft', timeLeft);
this.setFinalValue(this.formatTimeLeft(timeLeft), element);
}.bind(this)), refreshRate);

Expand Down
58 changes: 58 additions & 0 deletions tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,61 @@ test('Throws error when no data present', function() {
function() { timerElement.startTimer(); }, 'Errors when missing data'
);
});

asyncTest('Pauses on click when allowPause is true', function () {
expect(1);

var timerElement = $('#timer1');
timerElement.data('seconds-left', 3);
timerElement.startTimer({
onComplete: function() {
console.log('complete');
},
allowPause: true
}).trigger('click')

setTimeout(function() {
equal(timerElement.text(), '00:00:03', 'Timer is on pause');
start();
}, 3000);
});

asyncTest('Does NOT pause on click when allowPause is not specified', function () {
expect(1);

var timerElement = $('#timer1');
timerElement.data('seconds-left', 2);
timerElement.startTimer({
onComplete: function() {
console.log('complete');
},
// allowPause: true
}).trigger('click')

timerElement.on('complete', function(){
setTimeout(function() {
equal(timerElement.text(), '00:00:00', 'Cleared timer');
start();
}, 2000);
});
});

asyncTest('Does NOT pause on click when allowPause is false', function () {
expect(1);

var timerElement = $('#timer1');
timerElement.data('seconds-left', 2);
timerElement.startTimer({
onComplete: function() {
console.log('complete');
},
allowPause: false
}).trigger('click')

timerElement.on('complete', function(){
setTimeout(function() {
equal(timerElement.text(), '00:00:00', 'Cleared timer');
start();
}, 2000);
});
});