LukeD1uk LukeD1uk - 3 months ago 20
Javascript Question

Beautifying a countup timer

I am trying to recreate a Javascript timer I saw on this website: http://www.everysecond.io/apple/

Specifically the

**"It has been 1 minute and 32 seconds"**


I have a normal counter but it displays like a digital clock, I've been googling for an hour now and have no luck finding a solution.

Here is a pen with the counter from that website, I was trying to learn from it and strip it down to just what I need but with no luck. http://codepen.io/LukeD1uk/pen/ZOwvqv

Help would be greatly appreciated.

Answer
   var sec =0, min=0, hour=0;
setInterval(function(){
   sec++;
   if(sec==60){sec=0; min++;}
   if(min==60){min=0; hour++;}
   var str = 'it has been';
   str+= hour?' ' +hour + (hour==1?' hour':' hours'):'';
   str+= min?(hour?' and ':' ') +min+ (min==1?' minute':' minutes') :'';
   str+= sec?(min||hour?' and ':' ') +sec+ (sec==1?' second':' seconds'):'';
document.getElementById('passed').innerHTML=str;
},1000);

Here is the DEMO

Comments