0

I need a count up that increases by $30 million per day beginning Oct 1, 2013. My code below is based on a post here

code in head:

window.onload=function(){
var amount = document.getElementById('amount');
var start = new Date("October 1, 2013 00:00:00").getTime();
var current;
update();

function update() {
var current = (new Date().getTime() - start)/1000*147.22222222;
amount.innerText = formatMoney(current);
}

setInterval(update,1000);

function formatMoney(amount) {
var dollars = Math.floor(amount).toString().split('');
var cents = (Math.round((amount%1)*100)/100).toString().split('.')[1];
if(typeof cents == 'undefined'){
    cents = '00';
}else if(cents.length == 1){
    cents = cents + '0';
}
var str = '';
for(i=dollars.length-1; i>=0; i--){
    str += dollars.splice(0,1);
    if(i%3 == 0 && i != 0) str += ',';
}
return '$' + str;
}
}

code in body:

<div id='amount'></div>

Two things are wrong. It doesn't work in Firefox (neither does the code it's based on). And the total should be over $60 million by now but it's only around $30 million. Any help is appreciated.

Community
  • 1
  • 1
user2842596
  • 3
  • 1
  • 4

2 Answers2

1

By changing amount.innerText to amount.innerHTML this is the result. http://jsfiddle.net/X3hSH/

Ian Brindley
  • 2,197
  • 1
  • 19
  • 28
0

I have just changed your logic of calculating the difference of date from 1st october a little bit and it works

//3600 * 1000 milli seconds in 1 hour
//24 * 3600 * 1000 milli seconds in 1 day
var current = ((new Date()-start)/(24*3600*1000)); 
//multiply by 30 million * number of diff in days
current = current * 3000000;

About the second problem

innerText is a propriety IE thing. The W3C defines textContent as the official property

You should be able to do something like this

if(amount.innerText){
  amount.innerText = formatMoney(current);
}
else{
  amount.textContent = formatMoney(current);
}

Or use directly JQuery and you do not have to worry about different browser behaviour

JSFIDDLE

Anand
  • 14,545
  • 8
  • 32
  • 44
  • innerHTML did the trick. Thanks for changing the logic, Anand! It worked. I had to add a zero to 3000000 to make it 30000000. – user2842596 Oct 03 '13 at 13:50