Results 1 to 4 of 4

Thread: JS Global

  1. #1
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default JS Global

    I'm using the JS variables wrong again but I'm not sure how to correct it.

    Code:
    <script type="text/javascript">
    var i = 10;
    function num(number) {
    	if (number < 0) 
    		number--;
    	else 
    		number = 10;
    	$("#tempcontainer").append(number);
    	return number;
    }
    window.setInterval("num(i)", 1000);
    </script>
    <div id="tempcontainer"></div>
    Basically I just want it to count every 10 seconds and write the seconds to a div.
    Corrections to my coding/thoughts welcome.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    There are other problems, but number will never be less than 0.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Okay, fiddled around some more and got it working.

    Code:
    <script type="text/javascript">
    function num(number) {
    	if (number > 1) 
    		number--;
    	else 
    		number = 10;
    	$("#tempcontainer").append(number);
    	i = number;
    }
    var i = 10;
    window.setInterval("num(i)", 1000);
    </script>
    <div id="tempcontainer"></div>
    There also is a jquery call. I figure this could do this more efficiently in jquery or without jquery, all DOM.
    Corrections to my coding/thoughts welcome.

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I'd do something like:

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
    <script type="text/javascript">
    jQuery(function($){
    	var tc = $('#tempcontainer').append('10'), num = 10, prefix = ', ';
    	setInterval(function(){
    		tc.append(prefix + (--num));
    		if(num < 1){
    			num = 11;
    		}
    	}, 1000);
    });
    </script>
    </head>
    <body>
    <div id="tempcontainer"></div>
    </body>
    </html>
    If you wanted to do without jQuery:

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    </head>
    <body>
    <div id="tempcontainer"></div>
    <script type="text/javascript">
    (function(){
    	var tc = document.getElementById('tempcontainer'),
    	tn = document.createTextNode('10'), num = 10, prefix = ', ', tnc;
    	tc.appendChild(tn);
    	setInterval(function(){
    		tc.appendChild(tnc = tn.cloneNode(false));
    		tnc.nodeValue = prefix + (--num);
    		if(num < 1){
    			num = 11;
    		}
    	}, 1000);
    })();
    </script>
    </body>
    </html>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

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