Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Sep 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question 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
    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

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Code:
    <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

  • Users who have thanked Philip M for this post:

    oesxyl (09-03-2008)

  • #3
    New to the CF scene
    Join Date
    Feb 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #4
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    Code:
    <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>
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph


  •  

    Posting Permissions

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