PDA

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

chrbar
09-03-2008, 09:16 PM
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

Philip M
09-03-2008, 09:48 PM

<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

Music_Provoc
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

cheers

rangana
02-06-2009, 10:11 AM
<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)