user2842596
user2842596

Reputation: 3

JS Money Counter

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.

Upvotes: 0

Views: 2191

Answers (2)

Anand
Anand

Reputation: 14915

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

Upvotes: 0

Insyte
Insyte

Reputation: 2236

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

Upvotes: 1

Related Questions