SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation Arrrrg!!! "undefinded" and then "NaN" Help?

    In the code below I have a variable called (uncreatively enough) "number". This variable is supposed to be incremented every 2 seconds, and as a test I "alert" it out to the screen.

    However, the first alert says "undefined", and all subsequent alerts say "NaN".

    I'm fairly new to JavaScript, but not to programming. I declare and initialize it in the head, and then increment it in the function. I have tried placing the declaration in the function, thinking it might be a scope problem, but I get the same alerts.

    Any boot in the right direction would be greatly appreciated!

    PHP Code:
    <html>
      <
    head>
        <
    title>Campaign Promises for 2004</title>
        <
    link href="style.css" rel="stylesheet" type="text/css">

        <
    script language="javascript" type="text/javascript">
          <!--
            var 
    dollars = new array(0123456789","":""$");
            var 
    number 9999;
            function 
    window_onload()
            {
              
    window.setInterval("updateDollars()"2000);
            }
          -->
        
    </script>

      </head>
      <body language="javascript" onload="return window_onload();">
        <div align="center">
          <table width="750" border="1">
            <tr>
              <td>
          <table width="750" border="0" cellpadding="0" cellspacing="0" summary="page table">
            <tr>
              <td colspan="5" height="50" background="graphics/logo.gif">
                &nbsp;
              </td>
            </tr>
            <tr class="nav" valign="top">
              <td height="20" width="375" background="graphics/timeBar.gif">
                <span class="moneySpent">Money Spent: $</span>
                <img name="1" src="graphics/placeHolder.gif">

                <script language="javascript" type="text/javascript">
                  <!--
                    var startDollars = 10000000;
                    function updateDollars()
                    {
                      alert('number = ' + number);
                      number++;
                      if( number > 9 ) number = 0;
                    }
                  -->
                </script>

              </td>
              <td align="center">
                nav
              </td>
              <td align="center">
                nav
              </td>
              <td align="center">
                nav
              </td>
              <td align="center">
                nav
              </td>
            </tr>
            <tr height="15">
              <td colspan="5" background="graphics/logoUnderbar.gif">
              </td>
            </tr>
          </table>
              </td>
            </tr>
          </table>
        </div>
      </body>
    </html> 
    John

  2. #2
    SitePoint Zealot Drew630's Avatar
    Join Date
    Nov 2001
    Location
    Maryland
    Posts
    175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, you were right about the scope. What it is, since you seperated your functions into different areas, the number variable was defined once, then lost all definition once the scope was left from the head area. What I did was pass number to your function for updating dollars. It gets rid of the alert errors, but I am not sure exactly what this code is meant to do, so I don't know if it is behaving the eay you want. If not, post again and I'll take a look.

    Here's the fixed code.

    Code:
    <html>
      <head>
    	<title>Campaign Promises for 2004</title>
    	<link href="style.css" rel="stylesheet" type="text/css">
    	<script language="javascript" type="text/javascript">
    	  <!--
    		var dollars = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ",", ":", "$" );
    		var number = 9999;
    		function window_onload()
    		{
    		  window.setInterval("updateDollars(number)", 2000);
    		}
    	  -->
    	</script>
      </head>
      <body language="javascript" onload="return window_onload();">
    	<div align="center">
    	  <table width="750" border="1">
    		<tr>
    		  <td>
    	  <table width="750" border="0" cellpadding="0" cellspacing="0" summary="page table">
    		<tr>
    		  <td colspan="5" height="50" background="graphics/logo.gif">
    			&nbsp;
    		  </td>
    		</tr>
    		<tr class="nav" valign="top">
    		  <td height="20" width="375" background="graphics/timeBar.gif">
    			<span class="moneySpent">Money Spent: $</span>
    			<img name="1" src="graphics/placeHolder.gif">
    	<script language="javascript" type="text/javascript">
    	  <!--
    	 var startDollars = 10000000;
    	 function updateDollars(number)
    	 {
    	   alert('number = ' + number);
    	   number++;
    	   if( number > 9 ) number = 0;
    	 }
    	  -->
    	</script>
    		  </td>
    		  <td align="center">
    			nav
    		  </td>
    		  <td align="center">
    			nav
    		  </td>
    		  <td align="center">
    			nav
    		  </td>
    		  <td align="center">
    			nav
    		  </td>
    		</tr>
    		<tr height="15">
    		  <td colspan="5" background="graphics/logoUnderbar.gif">
    		  </td>
    		</tr>
    	  </table>
    		  </td>
    		</tr>
    	  </table>
    	</div>
      </body>
    </html>
    Drew
    ~Drew

    There Is No Greater Joy Than Soaring High On The Wings Of Your Dreams, Except Maybe The Joy Of Watching A Dreamer Who Has Nowhere To Land But In The Ocean Of Reality.

  3. #3
    SitePoint Addict BenANFA's Avatar
    Join Date
    Apr 2003
    Location
    Bath, UK
    Posts
    353
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually this is php not JavaScript

    PHP Code:
            var dollars = new array(0123456789","":""$" ); 
    this is the JavaScript mannor of defining an array

    PHP Code:
            var dollars = [0123456789","":""$" ]; 
    This is the only problem in your code fix this and it works. This is beacuse the error stops the JavaScript parser from processing the next line so the variable number is not created.

    If your using IE as your test bed switch off "disable script debugging" while you are developping, it catches this error.

  4. #4
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    BenANFA - you're close

    The PHP syntax would be
    PHP Code:
    $dollars = array( ... ); 
    The javascript he's trying is close, but has a small typo. The array constructor (as are all constructors) is capitalized
    Code:
    var dollars = new Array( ... );
    The syntax you posted for array defining is an array literal, which is perfectly valid.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  5. #5
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh! Thanks guys! The small "a" in the "new array()" was spot on!

    Changed to "new Array()" works just fine!

    Thanks again!
    John


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
  •