Link to home
Start Free TrialLog in
Avatar of lean0nme
lean0nme

asked on

Live Money/Currency Counter (US Debt Clock)

Hi. I'm looking for a script to count money up including cents. Exactly like this website: http://costofwar.com/en/state/AL/congdistrict/06/ except I need it fancier with images as numbers and a background.

For example: I set it to start from $1,030,301.01 and it should count up every second by whatever amount. Also, it cannot reset on reload and should be live no matter who sees the page.

Thank you.
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

So do you have something already and a server process that stores the current amount?
Or is this a Gimme-the-codez question?
Avatar of lean0nme
lean0nme

ASKER

I guess it's a gimme-the-codez question lol.
My email address is in my profile as well as a HIRE ME button
Ok, let's try it this way if you could help I'd appreciate it. I have this code to do what I want, but I need to modify 2 things.

1. It gets the value using date. I want to specificy the value.
2. I want the numbers to be fancier and use images instead.

Could you help with those please? :)

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 
<html>
<head>
	<title>Amount Incrementer</title>
	
	<script type="text/javascript">
		function calcAmount() {
		      var dateNow = new Date();
		      var dateStart = new Date();
			  var incAmount = .01; // change to the amount by which you wish to increment
		      dateStart.setDate(1);
		      dateStart.setMonth(0);
		      dateStart.setHours(0);
		      dateStart.setMinutes(0);
		      dateStart.setSeconds(0);
		      dateStart.setMilliseconds(0);
		      t = setTimeout(calcAmount, 1);
		      document.getElementById('amount').innerHTML = "$" + CurrencyFormatted(incAmount*parseInt((dateNow - dateStart)/(1000)));
		}
		
	// function for formatting numbers in currency format
	function CurrencyFormatted(amount)
	{
		var i = parseFloat(amount);
		if (isNaN(i)) {
			i = 0.00;
		}
		var minus = '';
		if (i < 0) { 
			minus = '-'; 
		}
		i = Math.abs(i);
		i = parseInt((i + .005) * 100);
		i = i / 100;
		s = new String(i);
		if (s.indexOf('.') < 0) { 
			s += '.00'; 
		}
		if (s.indexOf('.') == (s.length - 2)) { 
			s += '0'; 
		}
		s = minus + s;
		s = CommaFormatted(s)
		return s;
	}
	
	// function for formatting numbers with embedded commas
	function CommaFormatted(amount)
	{
		var delimiter = ","; // replace comma if desired
		var a = amount.split('.',2)
		var d = a[1];
		var i = parseInt(a[0]);
		if (isNaN(i)) { 
			return ''; 
		}
		var minus = '';
		if (i < 0) { 
			minus = '-'; 
		}
		i = Math.abs(i);
		var n = new String(i);
		var a = [];
		while(n.length > 3)
		{
			var nn = n.substr(n.length-3);
			a.unshift(nn);
			n = n.substr(0,n.length-3);
		}
		if (n.length > 0) { 
			a.unshift(n); 
		}
		n = a.join(delimiter);
		if (d.length < 1) { 
			amount = n; 
		} else { 
			amount = n + '.' + d; 
		}
		amount = minus + amount;
		return amount;
	}
	</script>
	
</head>
 
<body onload="calcAmount();">
 
Current amount is: <span id="amount"></span>
 
</body>
</html>

Open in new window

Sure. I'll look tomorrow morning. NOW I'm off to celebrate my birthday.
Thanks and Happy birthday!
Thanks - I strongly advice against using images
Instead go wild with CSS.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 
<html>
<head>
  <title>Amount Incrementer</title>
<style type="text/css">
#amount { font-face:courier; font-weight:bold; color:teal; }
</style>  
  <script type="text/javascript">
  
  // function for formatting numbers in currency format
  function CurrencyFormatted(amount)
  {
    var i = parseFloat(amount);
    if (isNaN(i)) {
      i = 0.00;
    }
    var minus = '';
    if (i < 0) { 
      minus = '-'; 
    }
    i = Math.abs(i);
    i = parseInt((i + .005) * 100);
    i = i / 100;
    s = new String(i);
    if (s.indexOf('.') < 0) { 
      s += '.00'; 
    }
    if (s.indexOf('.') == (s.length - 2)) { 
      s += '0'; 
    }
    s = minus + s;
    s = CommaFormatted(s)
    return s;
  }
  
  // function for formatting numbers with embedded commas
  function CommaFormatted(amount)
  {
    var delimiter = ","; // replace comma if desired
    var a = amount.split('.',2)
    var d = a[1];
    var i = parseInt(a[0]);
    if (isNaN(i)) { 
      return ''; 
    }
    var minus = '';
    if (i < 0) { 
      minus = '-'; 
    }
    i = Math.abs(i);
    var n = new String(i);
    var a = [];
    while(n.length > 3)
    {
      var nn = n.substr(n.length-3);
      a.unshift(nn);
      n = n.substr(0,n.length-3);
    }
    if (n.length > 0) { 
      a.unshift(n); 
    }
    n = a.join(delimiter);
    if (d.length < 1) { 
      amount = n; 
    } else { 
      amount = n + '.' + d; 
    }
    amount = minus + amount;
    return amount;
  }
  var amount = 1030301.01; // load from server
  window.onload=function() {
    var delay = 100; // milliseconds    
    var incAmount = .01; // change to the amount by which you wish to increment
    var tId = setInterval(function() {
      document.getElementById('amount').innerHTML = "$" + CurrencyFormatted(amount+=incAmount);
    },delay)

  }
  </script>
  
</head>
 
<body>
 
Current amount is: <span id="amount"></span>
 
</body>
</html>

Open in new window

Any reason to use CSS instead of images? I want it to have like a "flip style" counter.

Also, the counter shouldn't reset after reload and should be "live" with everyone seeing the same number like on the http://costofwar.com/en/state/AL/congdistrict/06/ website.

Thanks for the help!
Yes, using images are VERY resource hungry when flipping - now we flip as ft as possible, with images we can perhaps flip once a second
It is up to you to have the page read the initial counter from a text file. It is not advisable to read the server counter more than once per page load.
You insert it where I suggested it
Can you please elaborate on that? In order for me to have a live counter, I would have to write to a txt file and read it constantly, no? How could I make it do it once refresh?
I do not know how precise this counter should be nor what the algorithm for the number. If it can be calculated, calculate it each time the page is loaded. If not have a script run on the server that updates a text file and read that file from the page that shows it to the user.

Otherwise WHO updates the counter? Each person that loads the page will call to the server every second or so? That could be very heavy.

Perhaps you tell me what exactly you are counting and how you get to that number and I can give better answers
It's not calculating anything. Just takes a number from a .txt file and displays it is good enough. The file needs to be updated every second to increment the number and I just need that number to be consistent with everyone who views the page.

So basically I could create a php script to constantly update a .txt file and read that number with the script you provided? Any ideas for doing that?
Sure
Not being a php guy I would change the bit where it says 1030301.01 to
<?php file_get("counter.txt"); ?>
Thanks. I can write a php script to constantly update counter.txt though a loop every second or so but wouldn't that load the server too much? Also, where would I put the script in order for it to run constantly?
ASKER CERTIFIED SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for all the help!