getMilliseconds() / 100
El problema es que el formato en que lo devuelve es 0,00. A efectos prácticos, siguen siendo milisegundos con una coma de por medio.
¿Como podría convertirlo en un único numero entero?
El contador es el siguiente (muestra el resultado en un div id="counter"):
Código Javascript:
Ver original
/********************************************************************************************** * CountUp script by Praveen Lobo (http://PraveenLobo.com/techblog/javascript-countup-timer/) * This notice MUST stay intact(in both JS file and SCRIPT tag) for legal use. * http://praveenlobo.com/blog/disclaimer/ **********************************************************************************************/ function CountUp(initDate, id){ this.beginDate = new Date(initDate); this.countainer = document.getElementById(id); this.numOfDays = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ]; this.borrowed = 0, this.years = 0, this.months = 0, this.days = 0; this.hours = 0, this.minutes = 0, this.seconds = 0, this.milliseconds = 0; this.updateNumOfDays(); this.updateCounter(); } CountUp.prototype.updateNumOfDays=function(){ var dateNow = new Date(); var currYear = dateNow.getFullYear(); if ( (currYear % 4 == 0 && currYear % 100 != 0 ) || currYear % 400 == 0 ) { this.numOfDays[1] = 29; } var self = this; setTimeout(function(){self.updateNumOfDays();}, (new Date((currYear+1), 1, 2) - dateNow)); } CountUp.prototype.datePartDiff=function(then, now, MAX){ var diff = now - then - this.borrowed; this.borrowed = 0; if ( diff > -1 ) return diff; this.borrowed = 1; return (MAX + diff); } CountUp.prototype.addLeadingZero1=function(value){ return value < 100 ? ("0" + value) : value; } CountUp.prototype.addLeadingZero2=function(value){ return value < 10 ? ("0" + value) : value; } CountUp.prototype.formatTime=function(){ this.milliseconds = this.addLeadingZero1(this.milliseconds); this.milliseconds = this.addLeadingZero2(this.milliseconds); } CountUp.prototype.calculate=function(){ var currDate = new Date(); var prevDate = this.beginDate; this.milliseconds = this.datePartDiff(prevDate.getMilliseconds(), currDate.getMilliseconds(), 1000); this.seconds = this.datePartDiff(prevDate.getSeconds(), currDate.getSeconds(), 60); this.minutes = this.datePartDiff(prevDate.getMinutes(), currDate.getMinutes(), 60); this.hours = this.datePartDiff(prevDate.getHours(), currDate.getHours(), 24); this.days = this.datePartDiff(prevDate.getDate(), currDate.getDate(), this.numOfDays[currDate.getMonth()]); this.months = this.datePartDiff(prevDate.getMonth(), currDate.getMonth(), 12); this.years = this.datePartDiff(prevDate.getFullYear(), currDate.getFullYear(),0); } CountUp.prototype.updateCounter=function(){ this.calculate(); this.formatTime(); this.countainer.innerHTML = " <font face='digitaldreamfat' color='lime'>" + this.years + "</font> <small> <font color='white'>" + (this.years == 1? "Año" : "Años") + "</small></font>" + " <font face='digitaldreamfat' color='lime'>" + this.months + "</font> <small> <font color='white'>" + (this.months == 1? "Mes" : "Meses") + "</small></font>" + " <font face='digitaldreamfat' color='lime'>" + this.days + "</font> <small> <font color='white'>" + (this.days == 1? "Día" : "Días") + "</small></font>" + " <font face='digitaldreamfat' color='lime'>" + this.hours + "</font> <small> <font color='white'>" + (this.hours == 1? "Hora" : "Horas") + "</small></font>" + " <font face='digitaldreamfat' color='lime'>" + this.minutes + "</font> <small> <font color='white'>" + (this.minutes == 1? "Minuto" : "Minutos") + "</small></font>" + " <font face='digitaldreamfat' color='lime'>" + this.seconds + "</font></strong> <small> <font color='white'>" + (this.seconds == 1? "Segundo" : "Segundos") + "</small></font>" + " <font face='digitaldreamfat' color='lime'>" + this.milliseconds + "</font> <small> <font color='white'>" + "Centésimas" + "</small></font>"; var self = this; setTimeout(function(){self.updateCounter();}, 10); } window.onload=function(){ new CountUp('June 2, 2011 13:30:00 GMT+0200', 'counter'); }
Quiero prescindir de los milisegundos y así poder bajar el intervalo de actualización, sin que por ello el cronómetro pierda precisión en tiempo real. El rendimiento de ciertos navegadores se ve muy afectado por este script. No lo vais a creer, pero el que mejor lo soporta es el IE