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