1

我有一个简单的幻灯片脚本,它以给定的时间间隔旋转图像。它具有用于浏览幻灯片的“上一个”和“下一个”按钮。单击这些按钮后,将调用 clearInterval 并设置一个新计时器,但问题是 clearInterval 函数似乎不起作用,并且计时器只是堆积起来。

这是脚本:

        $(document).ready(function() {
        var currentPosition = 0;
        var slideWidth = 570;
        var slides = $(".slide");
        var slidesNumber = slides.length;
        var slideshowInterval;
        var speed = 5000;

        slideShowInterval = setInterval(changePosition, speed);

        slides.wrapAll('<div id="slides_holder"></div>');

        $("#slides_holder").css("width", slideWidth*slidesNumber);

        $("#left_arrow").click(function() {
            if (currentPosition == 0) {
                currentPosition = slidesNumber-1;
            }
            else {
                currentPosition--;
            }

            clearInterval(slideshowInterval);
            slideShowInterval = setInterval(changePosition, speed);
            moveSlide();
        });

        $("#right_arrow").click(function() {
            if (currentPosition == slidesNumber-1) {
                currentPosition = 0;
            }
            else {
                currentPosition++;
            }

            clearInterval(slideshowInterval);
            slideShowInterval = setInterval(changePosition, speed);
            moveSlide();
        });

        function changePosition() {
            if (currentPosition == slidesNumber-1) {
                currentPosition = 0;
            } else {
                currentPosition++;
            }
            moveSlide();
        }

        function moveSlide() {
            $("#slides_holder").animate({"marginLeft": slideWidth*(-currentPosition)});
        }
    });

有人能指出我的错误吗?提前致谢!

4

1 回答 1

6

的拼写错误slideshowInterval

您正在使用slideShowInterval的某个地方和某些地方slideshowInterval。使用任何一种。

于 2012-05-16T16:11:26.423 回答