473,399 Members | 2,278 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

stop "undefined" when looping through array with setInterval

How do I stop displaying "undefined" when using the code below.

The problem occurs as I want to display 5 items per page using
setInterval but this causes 'mycars[num]' to reach a value that doesn't
exist eg.

First time through loop would display

Ford
Vauxhall
Fiat
Saab
BMW

Second time through loop would display

Mercedes
Daewoo
undefined
undefined
undefined

Many thanks,

Chris
<script language="JavaScript" type="text/javascript">

onload = start;
//Array of Items
var mycars=new Array()
mycars[0]="Ford"
mycars[1]="Vauxhall"
mycars[2]="Fiat"
mycars[3]="Saab"
mycars[4]="BMW"
mycars[5]="Mercedes"
mycars[6]="Daewoo"
//no. spans in html
iSpan = 5
//no of refreshes before rediret
iRefs = 10
//Current refresh no.
iCur = 0
//Start num at 0
num=0
//Change array items every 5s

function start()
{
setInterval("looper()", 1000);
}

//Display items from array
function looper()
{

iCur++;

if(num>=mycars.length)
{
if(iCur>=iRefs)
{
window.location="index.html";
}
num=0;
}

for(i=1;i<=iSpan;i++)
{
if(num>mycars.length){
num=0;
} else {
display("b"+i, mycars[num]);
num++;
}
}
}

function display(id, str)
{
document.all[id].innerHTML = str;
}

Nov 11 '05 #1
1 3457
Lee
Chris said:

How do I stop displaying "undefined" when using the code below.

The problem occurs as I want to display 5 items per page using
setInterval but this causes 'mycars[num]' to reach a value that doesn't
exist eg.
if(num>mycars.length){


When num=mycars.length, you have reached a value tht doesn't exist.

Nov 11 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Don Vaillancourt | last post by:
What's going on with Javascript. At the beginning there was the "undefined" value which represented an object which really didn't exist then came the null keyword. But yesterday I stumbled...
25
by: Nitin Bhardwaj | last post by:
Well, i'm a relatively new into C( strictly speaking : well i'm a student and have been doing & studying C programming for the last 4 years).....and also a regular reader of "comp.lang.c" I...
49
by: matty | last post by:
Hi, I recently got very confused (well that's my life) about the "undefined" value. I looked in the FAQ and didn't see anything about it. On...
9
by: Klaus Johannes Rusch | last post by:
IE7 returns "unknown" instead of "undefined" when querying the type of an unknown property of an object, for example document.write(typeof window.missingproperty); Has "unknown" been defined...
3
by: car6006 | last post by:
Why am I getting the "undefined" in my txt box, I have tried everything! I am new here so I truly hope you are able to assist me... Thanks <html> <head> <title>Random Proverbs</title>...
15
by: bill | last post by:
I am trying to write clean code but keep having trouble deciding when to quote an array index and when not to. sometimes when I quote an array index inside of double quotes I get an error about...
8
by: Soneji | last post by:
Hello all! ( again ) Once more, I have a problem that seems unsolvable by me. I'm getting the, seemingly common, "undefined reference" linking error. I've tried quite a few things, but...
3
by: gsuns82 | last post by:
HI all, I tried to read an array from an external javascript file called read.js,The array was declared & as well as defined with some values inside the <script> tag of a html file. For an...
5
by: Pseudonyme | last post by:
Dear All : Ever had an httpd error_log bigger than the httpd access log ? We are using Linux-Apache-Fedora-Httpd 2006 configuration. The PHP lines code that lead too tons of errors are : ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.