Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

Thread: Need help to modify a formula for calculating in Javascript ?

1. Need help to modify a formula for calculating in Javascript ?

Hello,

I would like to reproduce the counter present at the top of:
http://www.ensembleverslavenir.ca/en/home.asp

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?

<script language="JavaScript" type="text/javascript">
var sTotal = 0;

function gazCount(start){
sTotal=sTotal+(0.1);
document.getElementById("counter").innerHTML = r2(sTotal*2.91);
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
}
var arg=nom+"=";
var alen=arg.length;
var i=0;
while (i<clen){
var j=i+alen;
if (i==0) break;
}
return false;
}
}

</script>

Thanks a lot,
Chris

• Code:
```<body onload = "IncCounter()">

<style>
.styling{
background-color:black;
color:lime;
font: bold 24px MS Sans Serif;
}
</style>

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

<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);
}
</script>```

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

• Users who have thanked Philip M for this post:

oesxyl (09-03-2008)

• saving the values - maybe working from server time?

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

cheers

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

if(increment!=null && increment!="")
num = increment;
else
}

if (c_start!=-1) {
c_start=c_start + c_name.length+1;
if (c_end==-1)
}
}
return "";
}

var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}
</script>```

•

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•