eezar21
asked on
javascript counter year to date
Hi,
I'm trying to create a counter to put on our website to show how many units we have sold this year to date.
I have attached the code below. I have an average number of 1543 units sold per second, which would give me an approx number of 24 billion by end of June, but I can't seem to reach that number with what I have written. Also the number generated seems to always start with a rounded number which isn't correct.
Any pointers would be really helpful.
Many thanks
I'm trying to create a counter to put on our website to show how many units we have sold this year to date.
I have attached the code below. I have an average number of 1543 units sold per second, which would give me an approx number of 24 billion by end of June, but I can't seem to reach that number with what I have written. Also the number generated seems to always start with a rounded number which isn't correct.
Any pointers would be really helpful.
Many thanks
<html>
<head>
</head>
<body>
test counter
<div id="counter">
</div>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script>
// Set the unit values in milliseconds.
var msecPerMinute = 1000 * 60;
var msecPerHour = msecPerMinute * 60;
var msecPerDay = msecPerHour * 24;
// Set a date and get the milliseconds
var date = new Date('1/6/2013');
dateMsec = date.getTime();
// Set the date to January 1, at midnight, of the specified year.
date.setMonth(0);
date.setDate(1);
date.setHours(0, 0, 0, 0);
// Get the difference in milliseconds.
var interval = dateMsec - date.getTime();
//document.write (interval);
var counter = interval;
setInterval("timer()",1);
function timer() {
counter++;
$('#counter').text(counter);
}
</script>
Had a doubt in your code, you said you have a production of 1543 per second but that is nowhere included in your code.
I have changed it to the following, see if this is what you wanted.
I have changed it to the following, see if this is what you wanted.
<html>
<head>
</head>
<body>
test counter
<div id="counter">
</div>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script>
var unitsPerSec = 1543 ;
var unitsPerMilleSec = 1543/1000 ;
// Start of year date
var date = new Date('1/1/2013');
//Cur date
var curdate = new Date();
//millisec from bigin of year till now
var miliSecDiff = curdate.getTime() - date.getTime();
//Current production till now from start of year
var counter = Math.floor((miliSecDiff/1000)*unitsPerSec);
setInterval("timer()",1);
function timer() {
counter += unitsPerMilleSec;
$('#counter').text(Math.floor(counter));
}
</script>
ASKER
Thats great - to clarify it is 1543 per second!
Just on question though - I would expect the number to run up by about 1543 every second, and it does about 400 per second?
Thanks!
Just on question though - I would expect the number to run up by about 1543 every second, and it does about 400 per second?
Thanks!
use this..
What happens is that though we have given that the method timer() should be called after very millisecond, internally it will take more than a millisecond to complete the cycle of the call,increment,display & render hence the lesser count.
So change it to call the method ever 500 milisecond then it should be ok.
What happens is that though we have given that the method timer() should be called after very millisecond, internally it will take more than a millisecond to complete the cycle of the call,increment,display & render hence the lesser count.
So change it to call the method ever 500 milisecond then it should be ok.
<html>
<head>
</head>
<body>
test counter
<div id="counter">
</div>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script>
var unitsPerSec = 1543 ;
var unitsPerMilleSec = 1543/1000 ;
// Start of year date
var date = new Date('1/1/2013');
//Cur date
var curdate = new Date();
//millisec from bigin of year till now
var miliSecDiff = curdate.getTime() - date.getTime();
//Current production till now from start of year
var counter = Math.floor((miliSecDiff/1000)*unitsPerSec);
setInterval("timer()",500);
function timer() {
counter += unitsPerSec/2;
$('#counter').text(Math.floor(counter));
}
</script>
ASKER
is there any way to comma delineate the output number?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thats brilliant - thanks!!
ASKER