Skip to content
This repository has been archived by the owner on Aug 22, 2021. It is now read-only.

Added support for time-formated numbers (hh:mm:ss) #1

Open
wants to merge 1 commit 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions jquery.counterup.js 100644 → 100755
Expand Up @@ -33,6 +33,19 @@
var isFloat = /^[0-9]+\.[0-9]+$/.test(num);
var decimalPlaces = isFloat ? (num.split('.')[1] || []).length : 0;

var isTime = /[0-9]+:[0-9]+:[0-9]+/.test(num);

// Convert time to total seconds
if (isTime) {
var times = num.split(':');
var s = 0;
var m = 1;
while (times.length > 0) {
s += m * parseInt(times.pop(), 10);
m *= 60;
}
}

// Generate list of incremental numbers to display
for (var i = divisions; i >= 1; i--) {

Expand All @@ -44,6 +57,15 @@
newNum = parseFloat(num / divisions * i).toFixed(decimalPlaces);
}

// Add incremental seconds and convert back to time
if (isTime) {
newNum = parseInt(s / divisions * i)
var hours = parseInt(newNum / 3600 ) % 24;
var minutes = parseInt(newNum / 60 ) % 60;
var seconds = parseInt(newNum % 60, 10);
newNum = (hours < 10 ? "0" + hours : hours) + ":" + (minutes < 10 ? "0" + minutes : minutes) + ":" + (seconds < 10 ? "0" + seconds : seconds);
}

// Preserve commas if input had commas
if (isComma) {
while (/(\d+)(\d{3})/.test(newNum.toString())) {
Expand Down
2 changes: 1 addition & 1 deletion jquery.counterup.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.