Skip to content
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

Stopwatch #17

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
110 changes: 74 additions & 36 deletions demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,49 +15,59 @@ <h1>jQuery Timer Demo</h1>
<p>Project home: <a href="http://jchavannes.com/jquery-timer" target="_blank">http://jchavannes.com/jquery-timer</a></p>
<p><a href="res/demo.js" target="_blank">View Source</a></p>

<hr/><h3 style='margin-top:20px;'>Example 1 - Stopwatch (counts ups)</h3>
<span id="stopwatch">00:00:00</span>
<br/>

<h2>Usage</h2>

<hr/>

<h3>Stopwatch</h3>

<h4>Create a new stopwatch</h4>
<p>HTML</p>
<pre>&lt;div id='time'&gt;%lt;/div&gt;</pre>
<p>Javascript</p>
<pre>
var stopwatch = $.stopwatch(function(watch) {
$('#time').html(watch.getFormattedTime();
});</pre>

<br/>

<h2>Examples</h2>

<hr/>

<h3 style='margin-top:20px;'>Example 1 - Stopwatch</h3>
<p>
<input type='button' value='Play/Pause' onclick='Example1.Timer.toggle();' />
<input type='button' value='Stop/Reset' onclick='Example1.resetStopwatch();' />
<span id="stopwatch">00:00:00</span><br/>
<input type='button' value='Example1.toggle()' onclick='Example1.toggle()' />
<input type='button' value='Example1.stop()' onclick='Example1.stop()' />
</p>
<br/>
<pre>
var Example1 = $.stopwatch(function () {
var output = this.getFormattedTime() + " - <i>(" + this.getTime() + " milliseconds)</i>";
$("#stopwatch").html(output);
});</pre>
<br/>

<h3>Example 2 - Countdown Timer</h3>
<span id="countdown">05:00:00</span>
<h3>Example 2 - Countdown</h3>
<form id="example2form">
<input type='button' value='Play/Pause' onclick='Example2.Timer.toggle();' />
<input type='button' value='Stop/Reset' onclick='Example2.resetCountdown();' />
<input type='text' name='startTime' value='300' style='width:30px;' />
<span id="countdown">05:00:00</span><br/>
<input type='button' value='Example2.toggle()' onclick='Example2.toggle()' />
<input type='button' value='Example2.stop()' onclick='Example2.stop()' />
</form>
<pre>
var Example2 = $.countdown(function() {
$("#countdown").html(this.getFormattedTime());
if (this.isFinished()) {
alert("Example 2: Countdown complete!");
}
}, 20);</pre>
<br/>

<h3>Example 3 - Slideshow (preserves time remaining when paused)</h3>
<div class="galleryImages">
<img src='res/img/1.jpg' alt='' />
<img src='res/img/2.jpg' alt='' />
<img src='res/img/3.jpg' alt='' />
<img src='res/img/4.jpg' alt='' />
<img src='res/img/5.jpg' alt='' />
<img src='res/img/6.jpg' alt='' />
<img src='res/img/7.jpg' alt='' />
<img src='res/img/8.jpg' alt='' />
<img src='res/img/9.jpg' alt='' />
<img src='res/img/10.jpg' alt='' />
<img src='res/img/11.jpg' alt='' />
<img src='res/img/12.jpg' alt='' />
<img src='res/img/13.jpg' alt='' />
<img src='res/img/14.jpg' alt='' />
<img src='res/img/15.jpg' alt='' />
<img src='res/img/16.jpg' alt='' />
<img src='res/img/17.jpg' alt='' />
</div><br/>
<div id="timeRemaining"><br/></div>
<p class="example-three">
<input type='button' value='Play / Pause' onclick='Example3.toggleGallery();' />
</p>

<h3>Example 4 - The Basics (just a suped-up version of setTimeout)</h3>
<h3>Example 3 - The original jQuery Timer (basically a suped-up version of setTimeout)</h3>
<p>timer has been executed <span id='counter' style=''>0</span> times.</p>
<div class='example-four'>
<input type='button' value='timer.play()' onclick='timer.play();' /><br/>
Expand All @@ -74,11 +84,39 @@ <h3>Example 4 - The Basics (just a suped-up version of setTimeout)</h3>
<input type='button' value='timer.set({time:1000})' onclick='timer.set({time:1000});' /><br/>
<input type='button' value='timer.set({time:5000})' onclick='timer.set({time:5000});' />
</div>
<br/><pre style='margin-bottom:20px; padding:10px 15px;'>
<br/><pre>
var count = 0;
var timer = $.timer(function() {
$('#counter').html(++count);
});
timer.set({ time : 1000, autostart : true });</pre>


<h3>Example 4 - Slideshow (preserves time remaining when paused)</h3>
<div class="galleryImages">
<img src='res/img/1.jpg' alt='' />
<img src='res/img/2.jpg' alt='' />
<img src='res/img/3.jpg' alt='' />
<img src='res/img/4.jpg' alt='' />
<img src='res/img/5.jpg' alt='' />
<img src='res/img/6.jpg' alt='' />
<img src='res/img/7.jpg' alt='' />
<img src='res/img/8.jpg' alt='' />
<img src='res/img/9.jpg' alt='' />
<img src='res/img/10.jpg' alt='' />
<img src='res/img/11.jpg' alt='' />
<img src='res/img/12.jpg' alt='' />
<img src='res/img/13.jpg' alt='' />
<img src='res/img/14.jpg' alt='' />
<img src='res/img/15.jpg' alt='' />
<img src='res/img/16.jpg' alt='' />
<img src='res/img/17.jpg' alt='' />
</div>
<p class="example-three">
<span id="timeRemaining"><br/></span>
<input type='button' value='Play / Pause' onclick='Example4.toggleGallery();' />
</p>


</body>
</html>
219 changes: 217 additions & 2 deletions jquery.timer.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@

;(function($) {

$.timer = Timer;
$.timer = Timer;
$.stopwatch = Stopwatch;
$.countdown = Countdown;

$.timerUtil = {
pad: pad,
formatTime: formatTime
};

/**
* First parameter can either be a function or an object of parameters.
Expand Down Expand Up @@ -273,5 +280,213 @@
return this;

};


/**
* @param {function=} updateFunction
* @param {boolean=} autostart
*/
function Stopwatch(updateFunction, autostart) {

if (typeof this == "function" || this.init) {
return new Stopwatch(updateFunction, autostart);
}

this.set(updateFunction, autostart);

return this;
}

/**
* @param {function=} updateFunction
* @param {boolean=} autostart
*/
Stopwatch.prototype.set = function(updateFunction, autostart) {

if (typeof updateFunction != "function") {
return;
}

this.init = true;
this.updateFunction = updateFunction;
this.startTime = 0;
this.pauseStart = 0;

if (autostart) {
this.startTime = new Date().getTime();
this.setInterval();
}

};

Stopwatch.prototype.timeUpdate = function() {

if (this.startTime == 0) {
this.timePassed = 0;
}
else {
this.timePassed = new Date().getTime() - this.startTime;
}

this.updateFunction.apply(this);

};

/**
* @returns {int}
*/
Stopwatch.prototype.getTime = function() {
return this.timePassed > 0 ? this.timePassed : 0;
};

/**
* @returns {string}
*/
Stopwatch.prototype.getFormattedTime = function() {
return formatTime(this.getTime());
};

Stopwatch.prototype.stop = function() {
this.clearInterval();
this.startTime = 0;
this.timeUpdate();
};

Stopwatch.prototype.pause = function() {
this.clearInterval();
this.pauseStart = new Date().getTime();
this.timeUpdate();
};

Stopwatch.prototype.toggle = function() {
if (this.pauseStart || this.startTime == 0) {
this.start();
}
else {
this.pause();
}
};

Stopwatch.prototype.setInterval = function() {

this.clearInterval();
this.interval = setInterval(interval, 1000/30);

var self = this;
function interval() {
self.timeUpdate();
}

};

Stopwatch.prototype.clearInterval = function() {
clearInterval(this.interval);
};

Stopwatch.prototype.start = function() {

if (!this.startTime) {
this.startTime = new Date().getTime();
}
else if (this.pauseStart) {
this.startTime += (new Date().getTime() - this.pauseStart);
this.pauseStart = 0;
}
else {
return;
}

this.setInterval();

};

/**
* @param {function=} updateFunction
* @param {int=} countdown
*/
function Countdown(updateFunction, countdown) {

if (typeof this == "function" || this.init) {
return new Countdown(updateFunction, countdown);
}

this.set(updateFunction, countdown);

return this;
}

Countdown.prototype = new Stopwatch();

/**
* @param {int} countdown
*/
Countdown.prototype.setCountdown = function(countdown) {
this.countdown = countdown * 1000;
};

/**
* @param {function=} updateFunction
* @param {int=} countdown
*/
Countdown.prototype.set = function(updateFunction, countdown) {
this.setCountdown(countdown);
Stopwatch.prototype.set.apply(this, [updateFunction]);
};

/**
* @returns {int}
*/
Countdown.prototype.getTime = function() {

var time = this.countdown - Stopwatch.prototype.getTime.apply(this);

if (time <= 0) {
time = 0;
this.finished = true;
this.startTime = 0;
this.clearInterval();
}
else {
this.finished = false;
}

return time;

};

/**
* @returns {boolean}
*/
Countdown.prototype.isFinished = function() {
return this.finished;
};

Countdown.prototype.start = function() {
if (this.getTime() == 0) {
this.startTime = 0;
}
Stopwatch.prototype.start.apply(this);
};

/**
* @param {int} number
* @param {int} length
* @returns {string}
*/
function pad(number, length) {
var str = '' + number;
while (str.length < length) {str = '0' + str;}
return str;
}

/**
* @param {int} time (in milliseconds)
* @returns {string}
*/
function formatTime(time) {
var min = parseInt(time / 60000),
sec = parseInt(time / 1000) - (min * 60),
hundredths = pad(parseInt(time / 10 - (sec * 100) - (min * 6000)), 2);
return (min > 0 ? pad(min, 2) : "00") + ":" + pad(sec, 2) + ":" + hundredths;
}

})(jQuery);
Loading