[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Need a countup counter that counts up money

Posted on 2008-10-12
4
Medium Priority
?
612 Views
Last Modified: 2012-05-05
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
Comment
Question by:superphonic
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 13

Expert Comment

by:TechTiger007
ID: 22697549
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
 

Author Comment

by:superphonic
ID: 22697607
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
 
LVL 13

Expert Comment

by:TechTiger007
ID: 22698026
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
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 2000 total points
ID: 22702408
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The most up-to-date version of this article is on my Blog https://iconoun.com/blog/
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question