因为imgIndex=3的时候if(!fImgs[imgIndex]||imgIndex==showImgIndex)
        return false;
停止循环了var fImgs = new Array();
fImgs[0] = {img:"2008929105038359.jpg",href:"2008102892246468.html"};
fImgs[1] = {img:"200892917107203.jpg",href:"2008102892246468.html"};
fImgs[2] = {img:"20081020143311125.jpg",href:"2008102892246468.html"};
var imgTimer;
var showImgIndex=-1;
function ShowImg(){
    var _flash_div =document.getElementById("DivScroll");
//问题就出在这个判断这里,大小比较不了,都是直接跳过
//imgIndex 用户传入的参数,fImgs.length数组长度

    if(showImgIndex>=fImgs.length)
        showImgIndex=0;
    if(!fImgs[showImgIndex])
        return false;
    var _liIndex ="_aImg"+showImgIndex;
    var img =new Image();
    img.border = "0";
    img.src = fImgs[showImgIndex].img;
    img.width = "173";
    img.height = "131";
    img.className="jpadpic";
    var _a =document.createElement("a");
    _a.setAttribute("target","_blank");
    _a.setAttribute("id",_liIndex);
    _a.setAttribute("href",fImgs[showImgIndex].href);
    _a.appendChild(img);
    _flash_div.innerHTML="";
    _flash_div.appendChild(_a);    return true;
}function imagePlayer()
{
    debugger;
    if(imgTimer)return;
    if(showImgIndex>fImgs.length)
    {
        showImgIndex =-1;
    }
    ShowImg(++showImgIndex);
    imgTimer =setInterval(function(){var stat = ShowImg(++showImgIndex);if(!stat){stop();}},3000);
}function stop()
{
    clearInterval(imgTimer);
    imgTimer = null;
}//function:get element by its Id
function $(e) 

   return document.getElementById ? document.getElementById(e) : null;
}

解决方案 »

  1.   

    代码没有错, 关键不清楚你要做什么?
    imgIndex 应该是你上面数组的索引值吧??
    fImgs.length 就是数组的长度.我想不出什么情况下 索引值会大于数组的长度,如果出现,很明显是出错了.
    正常来说, 代码每次都跳过是正确的.
      

  2.   

    改成这样看看alert(imgIndex)
    alert(fImgs.length)
    alert(imgIndex>fImgs.length)
        if(imgIndex>fImgs.length){
            imgIndex=0;
        }
    alert(imgIndex)
      

  3.   

    1楼的这个我知道,但你知道为什么会出现在个原因吗?
    数组长度为2,当imgIndex=3时,执行到该判断的时候
        if(imgIndex>fImgs.length)//(3 > 2){imgIndex=0;}
            imgIndex=0;
    因为进行大小比较时不成功,而没有将imgIndex重置为0的原因。
    我要问的其实是这个,请看清楚我的测试过程,谢谢你的回答...
      

  4.   

    if(imgIndex>fImgs.length)//这里的判断有问题,如果未大于但等于fImgs.length时,fImgs[imgIndex]就是null了,这样下面的语句就返回false,而你的计时器执行的匿名函数中判断了如果返回false就清楚计时器了
            imgIndex=0;
    //当imgIndex==fImgs.length时已经超过数组的最大索引了,所以!fImgs[imgIndex]就是true了
        if(!fImgs[imgIndex]||imgIndex==showImgIndex)
            return false;
      

  5.   

    相应2楼号召,现将代码全贴上<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>Test</title>
    <script type="text/javascript">
    var fImgs = new Array();
    fImgs[0] = {img:"http://www.mayiyy.com/ad_info/ad_pic_flash/index_jpzs/2008929105038359.jpg",href:"2008102892246468.html"};
    fImgs[1] = {img:"http://www.mayiyy.com/ad_info/ad_pic_flash/index_jpzs/200892917107203.jpg",href:"2008102892246468.html"};
    fImgs[2] = {img:"http://www.mayiyy.com/ad_info/ad_pic_flash/index_jpzs/20081020143311125.jpg",href:"2008102892246468.html"};
    var imgTimer;
    var showImgIndex=-1;function ShowImg(imgIndex){
        var _flash_div =document.getElementById("DivScroll");
        if(imgIndex>fImgs.length)
            imgIndex=0;
        if(!fImgs[imgIndex]||imgIndex==showImgIndex)
            return false;
        var _liIndex ="_aImg"+imgIndex;
        var img =new Image();
        img.border = "0";
        img.src = fImgs[imgIndex].img;
        img.width = "173";
    img.height = "131";
    img.className="jpadpic";
    var _a =document.createElement("a");
        _a.setAttribute("target","_blank");
        _a.setAttribute("id",_liIndex);
        _a.setAttribute("href",fImgs[imgIndex].href);
        _a.appendChild(img);
        _flash_div.innerHTML="";
    _flash_div.appendChild(_a);
    showImgIndex =imgIndex;
    return true;
    }function imagePlayer()
    {
        if(imgTimer)return;
        if(showImgIndex>fImgs.length)
        {
            showImgIndex =-1;
        }
        ShowImg(showImgIndex+1);
        imgTimer =setInterval(function(){var stat = ShowImg(showImgIndex+1);if(!stat){stop();}},3000);
    }function stop()
    {
    clearInterval(imgTimer);
    imgTimer = null;
    }//function:get element by its Id
    function $(e) 

       return document.getElementById ? document.getElementById(e) : null;
    }
    </script>
    </head><body onload="imagePlayer();">
    <div class="box" >
    <div class="neirong">
    <div class="neirongr">
    <!-- 精品招商 -->
    <div class="jpad">
    <div class="no">
    <ul>
    <li><a id="_lia1" href="javascript:void(0);" onclick="ShowImg('0');" >[1]</a></li>
    <li><a id="_lia2" href="javascript:void(0);" onclick="ShowImg('1');" >[2]</a></li>
    <li><a id="_lia3" href="javascript:void(0);" onclick="ShowImg('2');" >[3]</a></li>
    </ul>
    </div>
    <div id="DivScroll"></div>
    </div>
    </div>
    <div class="clear"></div>
    </div>
    </div>
    </body>
    </html>
      

  6.   

    当fImgs是不连续的话就会出现问题.
      

  7.   

    lz,是你搞错了,参数的imgIndex和你用的imgIndex = 0 其实对于javascript来说不是同一个变量.
    你要给imgIndex赋值的话,要在函数开始用
    var imgIndex = imgIndex;
      

  8.   

    你要做的是不是循环显示?lengh-1不就行了嘛
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>Test</title>
    <script type="text/javascript">
    var fImgs = new Array();
    fImgs[0] = {img:"http://www.mayiyy.com/ad_info/ad_pic_flash/index_jpzs/2008929105038359.jpg",href:"2008102892246468.html"};
    fImgs[1] = {img:"http://www.mayiyy.com/ad_info/ad_pic_flash/index_jpzs/200892917107203.jpg",href:"2008102892246468.html"};
    fImgs[2] = {img:"http://www.mayiyy.com/ad_info/ad_pic_flash/index_jpzs/20081020143311125.jpg",href:"2008102892246468.html"};
    var imgTimer;
    var showImgIndex=-1;function ShowImg(imgIndex){
        var _flash_div =document.getElementById("DivScroll");
        if(imgIndex>fImgs.length-1)
            imgIndex=0;
        if(!fImgs[imgIndex]||imgIndex==showImgIndex)
            return false;
        var _liIndex ="_aImg"+imgIndex;
        var img =new Image();
        img.border = "0";
        img.src = fImgs[imgIndex].img;
        img.width = "173";
        img.height = "131";
        img.className="jpadpic";
        var _a =document.createElement("a");
        _a.setAttribute("target","_blank");
        _a.setAttribute("id",_liIndex);
        _a.setAttribute("href",fImgs[imgIndex].href);
        _a.appendChild(img);
        _flash_div.innerHTML="";
        _flash_div.appendChild(_a);
        showImgIndex =imgIndex;
        return true;
    }function imagePlayer()
    {
        if(imgTimer)return;
        if(showImgIndex>fImgs.length)
        {
            showImgIndex =-1;
        }
        ShowImg(showImgIndex+1);
        imgTimer =setInterval(function(){var stat = ShowImg(showImgIndex+1);if(!stat){stop();}},3000);
    }function stop()
    {
        clearInterval(imgTimer);
        imgTimer = null;
    }//function:get element by its Id
    function $(e) 

       return document.getElementById ? document.getElementById(e) : null;
    }
    </script>
    </head><body onload="imagePlayer();">
    <div class="box" >
    <div class="neirong">
    <div class="neirongr">
    <!-- 精品招商 -->
    <div class="jpad">
    <div class="no">
    <ul>
    <li><a id="_lia1" href="javascript:void(0);" onclick="ShowImg('0');" >[1]</a></li>
    <li><a id="_lia2" href="javascript:void(0);" onclick="ShowImg('1');" >[2]</a></li>
    <li><a id="_lia3" href="javascript:void(0);" onclick="ShowImg('2');" >[3]</a></li>
    </ul>
    </div>
    <div id="DivScroll"></div>
    </div>
    </div>
    <div class="clear"></div>
    </div>
    </div>
    </body>
    </html>
      

  9.   

    见鬼,code里面不能再变颜色啊
        if(imgIndex>fImgs.length-1)
            imgIndex=0;
      

  10.   

    你数组长度怎么会是2呢大哥,最大下标2,长度是3啊(3>3)==true?
      

  11.   

    应该是
     if(imgIndex >= fImgs.length)
            imgIndex=0;
    就ok了
      

  12.   


    其实我没认真看数组(数组是copy来的),调试的时候即时窗口中给的是2,就把他当2看了,所以当使用4楼的给的意见的时候,就知道自己错了
    呵呵.