[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 623
  • Last Modified:

Need a countup counter that counts up money

Hi all,

I have been trying to find a countup script that counts up cash but have been unsuccessfull.

Basically it needs to start at say £250,000 and then count up throughout the day by a total of say £3000 a day. It needs to count up by random amounts throughout the day like it is counting real time cash being added to it. So it will jump by say 20 pence, then in a few seconds jump by 7 pence, then jump by 1 pence etc... It needs to keep track of the cash amount so will need a start day so it can calculate when the page is loaded what the starting cash amount should be etc..

Any help appreciated.
0
superphonic
Asked:
superphonic
  • 2
1 Solution
 
TechTiger007Commented:
If you are using javascript alone to do what you are trying then you can keep track of the count only for that page instance. when you refresh the page or fetch the page again you will get the whole counting started again from 0 or what ever your initial data is.
If you want the count to go on for the day and if it should not be affected by when page is loaded and if it should be same all browser instances of this page then You cannot do this with javascript alone.

You can do this with AJAX.
1. Create a table that stores the count, time when last incremented
2. Create a web service that will fetch the count from this table, increment the count with a random number that should depend on the current time, last increment time and return the fetched data
3. When your page is loaded you can use ajax calls to update the page based on what frequency you want
0
 
superphonicAuthor Commented:
This seems like overkill to me, I thought it could be done with javascript alone. This is why it needs a start date and time in the script. So for example the script starts at £250,000 on 15th October 2008 at 15:43:00, and it increments at £3000 a day which is 0.035 a second, so as the day, week go on when the page is loaded it check the start date and time, calculates how much time has passed and adds that amount to the money, then it just counts up from there. The cash amount is always correct as of the day and time the browser loaded the script...  Hope that makes sense. No need for a database or anything
0
 
TechTiger007Commented:
mm... Then you can use getTime method in javascript which will give you the number of milliseconds from 01-01-1970. Use this in your javascript to calculate the amount

http://www.w3schools.com/jsref/jsref_getTime.asp

use the following method to update time in interval
<script type="text/javascript">
     
function UpdateAmount()
         {
var d = new Date();
//calculate and update here
         setTimeout("UpdateAmount()", <milliseconds>);
         }
         </script>
0
 
Michel PlungjanIT ExpertCommented:
Very realistic if I have to say this myself
<html>
  <head>
  <title></title>
<script>
/* random counter, (c) 2008 Michel Plungjan javascript(a)plungjan.name */
 
var startDay = new Date(2008,8,30); // 3th of September
var now = new Date();
var diff = now.getTime()-startDay.getTime()
var diffInDays = parseInt(diff/(24*60*60*1000))
var aDaysWorth=3000;
var startAmount = 250000
var todayStart = (startAmount+(aDaysWorth*(diffInDays-1)))*100; // in pence
var pencePerSecond = parseInt(300000/(24*60*60))/100;
var counter = 0;
 
function addComma(num,sep,decs) {
   var sep = (sep||',')
   var numStr = new String(num)
   var sign="";
   if (numStr.indexOf('-')==0) {
      sign='-'
      numStr=numStr.substring(1)
   }
   var dotPos = numStr.indexOf('.');
   var commaPos = numStr.indexOf(',');
   var dec ="";
   if (sep == "." && commaPos!=-1) {
     var parts = numStr.split(',')
     dec = ","+parts[1]
     numStr = parts[0]
   }
   else if (sep == "," && dotPos!=-1) {
     var parts = numStr.split('.')
     dec = "."+parts[1]
     numStr = parts[0]
   }
   else if (decs) dec = (sep==',')?".00":",00"
   numArr=numStr.split('').reverse();
   for (i=3;i<numArr.length;i+=3) {
     numArr[i]+=sep
   }
   return sign+numArr.reverse().join('')+dec;
}
 
function count() {
  counter++
  todayStart+=pencePerSecond;
  var rnd = parseInt(Math.random()*60);
  window.status=rnd+':'+counter;
  if ((counter > rnd && counter%rnd==0) || (counter < rnd && rnd%counter==0)) show();
  if (counter >=60) counter=1;
}
function show() {
  document.getElementById('cntSpan').innerHTML=addComma(todayStart.toFixed(2));
}
var tId;
window.onload=function() { 
  show(); 
  tId = setInterval('count()',1000); // count the money 
}
 
</script>
  </head>
  <body>
Pounds:<span id="cntSpan"></span>
  </body>
</html>

Open in new window

0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now