User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 397,763 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,549 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Jan 27th, 2007
Views: 4,379
hi i was set the task to increment a number by x every x seconds and i tried to find some code off the net, but found the subject covered very sparsley so for whoevers benefit ill post the code here hope it helps http://www.daniweb.com/code/../techt...icon_smile.gif

Thanks

Nicholas
Last edited : Jan 27th, 2007.
php Syntax
  1. <html>
  2. <head>
  3. <style type="text/css">
  4. div.cont {
  5. position: relative;
  6. background-image: url(counter.gif);
  7. width:160px;
  8. height:110px;
  9. vertical-align:text-bottom;
  10. }
  11. div.cont div.ans
  12. {
  13. position: absolute;
  14. bottom: 0px;
  15. margin-bottom:15px;
  16. margin-left:7px;
  17. color:black;
  18. font-family: Verdana, Tahoma, Sans-Serif;
  19. font-size: 15pt;
  20. line-height: normal;
  21. }
  22.  
  23. </style>
  24.  
  25. <?php
  26.  
  27. $now = time();
  28. $start = mktime(0, 0, 0, 1, 24, 2007);
  29. $carbonsaving =((($now - $start) * 0.0058774) + 130000);
  30. $format = round($carbonsaving, 2);
  31. // in this example
  32. // $now = a unix timestamp of this very second
  33. // $start is the date that you want the counter to start from sent over //as a unix timestamp
  34. // $carbonsaving is the calculation that you want to perform to get //your base figure
  35. // i.e. total saving = ((date now - start date)* growth rate) + base rate
  36. // this gives us the starting saving all that needs to be done is increment it with javascript
  37. ?>
  38.  
  39. <script type="text/javascript">
  40. // we need to import our server side variable into javascript to let it increment live
  41.  
  42. var car = <?php print($format); ?>;
  43. var rou
  44. function incs()
  45. {
  46. car = car + 0.01;
  47. rou = Math.round(car*100)/100
  48. document.getElementById("carb").innerHTML=rou;
  49. }
  50. // what function incs does is take car and adds 0.01 to it
  51. //rou rounds the figure to 2 dp
  52. //the document.getElementById("carb") can refer to a <p> tag //<span> or whatever and just says with .innerHTML=rou; that the //value between the results of rou
  53. //hope this helps
  54. //Nicholas King
  55. //ecotricity
  56. </script>
  57. </head>
  58. <!-- body onload setInterval tells the page to load our javascript function and repeat it by every x microseconds, so this repeats every 2 seconds //-->
  59. <body onload="setInterval('incs()', 2000)">
  60. <div class="cont">
  61. <div class="ans">
  62. <span id="carb">Calculating...</span>
  63. </div>
  64. </div>
  65. </body>
  66. </html>
Comments (Newest First)
binoj_daniel | DaniWeb Expert | Aug 17th, 2007
Nice stuff
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 4:00 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC