/**
* ---------------------------------------------------------------------------
* this jsFiddle uses some external helper scripting (the Helper
* pseudo namespace). See External Resources
* ---------------------------------------------------------------------------
**/
// initialize time
var endTime = new Date
,startTime = new Date
,secondsAhead = +(document.querySelector('input').value) || 600
,stopped = false;
// set the end time of the timer (in seconds from now)
endTime.setSeconds(endTime.getSeconds()+secondsAhead);
endTime.setMilliseconds(0);
// timer method, recursive. Whilst the current time
// is smaller than the projected end time [endTime]
// a timeout is initialized, running this method
// after a second (1000ms). The facto it is thus
// running for the duration of endTime - startTime.
function countDown(){
var diff = Math.ceil((endTime-new Date)/1000);
Helper.log();
Helper.log(String.Format(
'<b>{0}:{1}:{2}</b>'
,zeroPad(Math.floor(diff/3600))
,zeroPad(Math.floor((diff/60)%60))
,zeroPad(Math.floor((diff%60)))
)
);
if (!stopped && diff >= 1) {
setTimeout(countDown,1000);
} else {
Helper.log(
String.Format(
'<b>Done</b>:{2}<br>started: {0}<br>ended {1}'
,startTime
,new Date
,stopped ? '<br><i>stop button clicked</i>' : '')
)
}
}
// pad [nr] left with [nr].length - base [chr]'s (or '0')
function zeroPad(nr,base,chr){
var len = (String(base||10).length - String(nr).length)+1;
return len > 0? new Array(len).join(chr||'0')+nr : nr;
}
// stop button handler
document.querySelector('#stop').onclick = function(){
stopped = true;
clearTimeout(0);
}
// start button handler
document.querySelector('#start').onclick = function(){
stopped = true;
clearTimeout(0);
secondsAhead =...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Get Fiddle Pages with PRO
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.