JavaScript/Timer
These JavaScript functions format a given number of seconds as an hour-minute-second or minute-second time or only show hours when one hour is exceeded. The names of the functions are HMStimer()
and MStimer()
and mediaTimer()
respectively. The name of the third refers to the common use of this timer layout in multimedia player software.
Each of these functions is bundled with a separate function (_core
suffix) to be able to handle negative and invalid inputs. Should a negative number be entered, the time is returned with a prepended dash, and if no valid number is entered, "– –:– –:– –
" is returned. The thin spaces between the dashes are necessary to match the width of the digits.
The timer functions can be seen in action by running the media timer script on a web page with a video or an audio element.
Functions
edit// A dedicated parent object to store the variables used by the functions.
// This prevents interference with other scripts on the page that might be using a variable called "HH" or "MM" or "SS".
var media_time = {};
// HH:MM:SS timer
function HMStimer_core(seconds) {
// hours
media_time.HH = Math.floor( seconds/3600 );
// leading zero
if ( seconds < 36000 ) media_time.HH = "0" + media_time.HH;
// minutes
media_time.MM = Math.floor( seconds/60%60 );
// leading zero
if ( seconds%3600 < 600 ) media_time.MM = "0" + media_time.MM;
// seconds
media_time.SS = Math.floor( seconds%60 );
// leading zero
if ( seconds%60 < 10 ) media_time.SS = "0" + media_time.SS;
return media_time.HH+":"+media_time.MM+":"+media_time.SS;
}
function HMStimer(seconds) {
if (seconds >= 0) return HMStimer_core(seconds); // zero or positive
if (seconds < 0) // negative
{
seconds = seconds * (-1);
return "-"+HMStimer_core(seconds);
}
if (seconds == undefined || isNaN(seconds) ) return "– –:– –:– –";
}
// MM:SS timer
function MStimer_core(seconds) {
// minutes
media_time.MM = Math.floor( seconds/60 );
// leading zero
if ( seconds%3600 < 600 ) media_time.MM = "0" + media_time.MM;
// seconds
media_time.SS = Math.floor( seconds%60 );
// leading zero
if ( seconds%60 < 10 ) media_time.SS = "0" + media_time.SS;
return media_time.MM+":"+media_time.SS;
}
function MStimer(seconds) {
if (seconds >= 0) return MStimer_core(seconds); // zero or positive
if (seconds < 0) // negative
{
seconds = seconds * (-1);
return "-"+MStimer_core(seconds);
}
if (seconds == undefined || isNaN(seconds) ) return "– –:– –";
}
// generic timer which only shows hours when 3600 seconds are exceeded – commonly used in multimedia players
function mediaTimer_core(seconds) {
if ( seconds >= 3600 ) {
// hours
media_time.HH = Math.floor( seconds/3600 );
}
// minutes
media_time.MM = Math.floor( seconds/60%60 );
// leading zero
if ( seconds%3600 < 600 ) media_time.MM = "0" + media_time.MM;
// seconds
media_time.SS = Math.floor( seconds%60 );
// leading zero
if ( seconds%60 < 10 ) media_time.SS = "0" + media_time.SS;
if ( seconds >= 3600 ) {
return media_time.HH+":"+media_time.MM+":"+media_time.SS;
} else { return media_time.MM+":"+media_time.SS; }
}
function mediaTimer(seconds) {
if (seconds >= 0) return mediaTimer_core(seconds); // zero or positive
if (seconds < 0) // negative
{
seconds = seconds * (-1);
return "-"+mediaTimer_core(seconds);
}
if (seconds == undefined || isNaN(seconds) ) return "– –:– –";
}