View Full Version : Need help to modify a formula for calculating in Javascript ?

09-03-2008, 09:16 PM

I would like to reproduce the counter present at the top of:
I've updated the javascript by adding cookies (copy below).

But this counter calculates the tons of greenhouse gases were emitted.
My counter has to calculate the number of bottles and cans which have not been recycled.

Do you know how to change the formula for calculating to obtain an integer value (without comma) which equals 14 bottles per second?

My javascript (with cookies) is:

<script language="JavaScript" type="text/javascript">
var sTotal = 0;
if (lire_cookie('compteur'))
sTotal = parseFloat(lire_cookie('compteur'));

function gazCount(start){
document.getElementById("counter").innerHTML = r2(sTotal*2.91);
document.cookie="compteur=" + sTotal;
setTimeout("gazCount();", 100);
function r2(n){
ans = n * 1000
ans = Math.round(ans /10) + ""
while (ans.length < 3) {ans = "0" + ans}
len = ans.length
ans = ans.substring(0,len-2) + "." + ans.substring(len-2,len)
return ans
function lire_cookie(nom) {
var arg=nom+"=";
var alen=arg.length;
var clen=document.cookie.length;
var i=0;
while (i<clen){
var j=i+alen;
if (document.cookie.substring(i, j)==arg)
return arguments_cookies(j);
i=document.cookie.indexOf(" ",i)+1;
if (i==0) break;
return false;
function arguments_cookies(offset){
var endstr=document.cookie.indexOf (";", offset);
if (endstr==-1) endstr=document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));

window.onload = gazCount;

Thanks a lot,

Philip M
09-03-2008, 09:48 PM
<body onload = "IncCounter()">

font: bold 24px MS Sans Serif;
padding: 6px;

<p> Number of bottles and cans not recycled:- <span id="cntr" class="styling"></span>

<script type="text/javascript">
var num = 0;
function IncCounter() {
num = num + 14; // increment counter by 14 per second
document.getElementById("cntr").innerHTML = num;
t = setTimeout('IncCounter()', 1000);

You will have to add the cookies as required.

Quizmaster: In nature, what invertebrate has a name which literally means "one hundred feet"?
Contestant: Giraffe

02-06-2009, 09:54 AM
Thanks for this code Phillip M - do you have any advice on how to keep the values so they dont reset when refreshing the page? and for it to carry on counting so when you revisit the page you can see that the number has increased dramatically?

any help would be greatly appreciated


02-06-2009, 10:11 AM
<script type="text/javascript">
function IncCounter() {
var num = getCookie('increment');
num = Number(num) + 14; // increment counter by 14 per second
document.getElementById("cntr").innerHTML = num;
t = setTimeout('IncCounter()', 1000);

function checkCookie() {
var increment=getCookie('increment');
if(increment!=null && increment!="")
num = increment;

function getCookie(c_name) {
if (document.cookie.length>0) {
c_start=document.cookie.indexOf(c_name + "=");
if (c_start!=-1) {
c_start=c_start + c_name.length+1;
if (c_end==-1)
return unescape(document.cookie.substring(c_start,c_end));
return "";

function setCookie(c_name,value,expiredays) {
var exdate=new Date();
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString());

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum