stop "undefined" when looping through array with setInterval

C

Chris

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;
}
 
L

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top