调用函数没有响应

时间:2017-04-02 21:37:50

标签: javascript html

滑块的HTML代码

<body>
<button id="l"  onClick="imagem()"><b><</b></button>

<img  id="aaa">

<button id="r" onClick="image()"><b>></b></button>
</body>
</html>

JavaScript代码滑块

<script type="text/javascript">
    var aa = ["im1.jpg", "im2.jpg", "im3.jpg", "im4.jpg", "im5.jpg", "im6.jpg"];
    var a = 1;

    function image(n) {
        a = a + 1;
        imageShow(a);
    }

    function imagem(n) {
        a--;
        imageShow(a);
    }

    function imageShow(r) {
        if (r > aa.length) {
            a = 1;
        }
        if (r < 1) {
            a = aa.length;
        }
        document.getElementById("aaa").src = aa[a - 1];
    }

    imageShow(1); // Call to imageShow function not responding.


    auto(); // Call to auto function not responding.

    function auto() {
        if (a > aa.length) {
            a = 1;
        }
        document.getElementById("aaa").src = aa[a - 1];
        a++;
        setTimeout(auto, 5000);
    }
</script>

请有人帮我弄清楚这个问题。对function auto()的调用并调用“function imageShow”没有响应。

1 个答案:

答案 0 :(得分:2)

定义后,您应该调用auto()函数。

var aa = ["im1.jpg", "im2.jpg", "im3.jpg", "im4.jpg", "im5.jpg", "im6.jpg"];
var a = 1;

function image() {
    a++;
    imageShow(a);
}

function imagem() {
    a--;
    imageShow(a);
}

function imageShow(r) {
    if (r > aa.length) {
        a = 1;
    }
    if (r < 1) {
        a = aa.length;
    }
    document.getElementById("aaa").src = aa[a - 1];
}

function auto() {
    if (a > aa.length) {
        a = 1;
    }
   document.getElementById("aaa").src = aa[a - 1];
   a++;
   setTimeout(auto, 5000);
}
auto();