SitePoint Sponsor |
|
User Tag List
Results 1 to 5 of 5
-
May 6, 2003, 19:29 #1
- Join Date
- Jul 2002
- Location
- Along the Wasatch Fault line.
- Posts
- 1,771
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ",", ":", "$");
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">
</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
-
May 7, 2003, 07:30 #2
- 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"> </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
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.
-
May 7, 2003, 07:32 #3
- 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(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ",", ":", "$" );
PHP Code:var dollars = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ",", ":", "$" ];
If your using IE as your test bed switch off "disable script debugging" while you are developping, it catches this error.
-
May 7, 2003, 07:40 #4
- 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( ... );
Code:var dollars = new Array( ... );
-
May 7, 2003, 07:43 #5
- 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