看不清为什么setInterval()被清除了

时间:2013-08-28 00:28:21

标签: javascript jquery timer setinterval

我不能为我的生活看到为什么我的setInterval()函数只执行一次。我相信我的使用方法是正确的。这是一个幻灯片演示脚本,它将一组幻灯片作为幻灯片对象的一部分,称为TheBand,并使用setInterval()更改幻灯片。

这是计时器:

    current_band.obj_timer = window.setInterval(function () {
         TheBand.nextSlide(current_band_id);
    }, current_band.timer);

这是nextSlide函数:

    nextSlide : function (band_id) {

    var obj_band = TheBand.bands[band_id];

    if (band_id == 0) {
        console.log(obj_band.current_slide, obj_band.slide_count)
    }

    if (obj_band.current_slide + 1 >= obj_band.slide_count) {
        new_slide = 0;
    } else {
        new_slide = obj_band.current_slide + 1;
    }

    TheBand.changeSlide(band_id, new_slide);
},

然后是changeSlide函数:

    changeSlide : function (band_id, slide_id) {

    var obj_band = TheBand.bands[band_id];

    if (slide_id != obj_band.current_slide) {

        // Get the viewport width for display purposes
        var slide_width = jQuery(obj_band.viewport).width();

        // Set up the slide objects
        var current_slide = jQuery(obj_band.slides[obj_band.current_slide]);
        var new_slide = jQuery(obj_band.slides[slide_id]);

        // Set the left value for the new slide and show it
        new_slide.css('left', slide_width).width(slide_width).height(obj_band.max_height);
        new_slide.show();

        // Animate the slide transition
        current_slide.animate({ left: -slide_width }, obj_band.transition_time);
        new_slide.animate({ left: 0 }, obj_band.transition_time, 'swing');
        if (new_slide.css('background-color') != current_slide.css('background-color')) {
            jQuery(obj_band.back).animate({ backgroundColor : new_slide.css('background-color') }, obj_band.transition_time);
        }

        // Set the class for the nav buttons
        jQuery(obj_band.nav_buttons[obj_band.current_slide]).removeClass('selected');
        jQuery(obj_band.nav_buttons[slide_id]).addClass('selected');

        // Run the slide javascript - be careful with this!
        eval(obj_band.slide_script[obj_band.current_slide].unload);
        eval(obj_band.slide_script[slide_id].load);

        window.setTimeout(function () {
            eval(obj_band.slide_script[slide_id].ready);
        }, obj_band.transition_time);

        // Set the current slide variable
        obj_band.current_slide = slide_id;

        // Set the correct Nav color
        TheBand.setNavColor(band_id);

    }

},

看起来很简单,但我只能进入第二张幻灯片!?在控制台中没有错误或警告,如果我看到清除计时器断点,它确实显示了一个清晰的计时器事件,但它不是来自此代码...请帮助?

P.S。如果我注释掉changeSlide函数,setInterval()计时器会中断。

0 个答案:

没有答案
相关问题