...

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



chrbar
09-03-2008, 10:16 PM
Hello,

I would like to reproduce the counter present at the top of:
http://www.ensembleverslavenir.ca/en/home.asp
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){
sTotal=sTotal+(0.1);
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;
</script>

Thanks a lot,
Chris

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

<style>
.styling{
background-color:black;
color:lime;
font: bold 24px MS Sans Serif;
padding: 6px;
}
</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>


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

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

function checkCookie() {
var increment=getCookie('increment');
if(increment!=null && increment!="")
num = increment;
else
setCookie('increment',0,365);
}

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;
c_end=document.cookie.indexOf(";",c_start);
if (c_end==-1)
c_end=document.cookie.length;
return unescape(document.cookie.substring(c_start,c_end));
}
}
return "";
}

function setCookie(c_name,value,expiredays) {
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}
</script>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum