-1

我收到以下错误:TypeError: slides[i] is undefined。

这很奇怪,因为它不应该能够访问幻灯片变量吗?

<script>
    $('document').ready(function() {
        $.ajax({
            type: "POST",
            url: "getSlides.php",
            data: '',
            cache: false,
            success: function(response)
            {
                var slides = JSON.parse(response);
                for (var i = 0; i < slides.length; i++) {
                    setTimeout(function() {
                        if (slides[i].type === 'image') {
                            $('#slideshow').html('<img src="' + slides[i].image_video + '" />');
                        }
                    }, 2000);
                }
            }
        });
    });
</script>
4

5 回答 5

1

它看起来像循环问题中的经典闭包

$('document').ready(function () {
    $.ajax({
        type: "POST",
        url: "getSlides.php",
        dataType: 'json',
        cache: false,
        success: function (response) {
            $.each(slides, function (slide) {
                if (slide.type === 'image') {
                    $('#slideshow').html('<img src="' + slide.image_video + '" />');
                }
            })
        }
    });
});
于 2013-11-14T11:35:12.610 回答
1

您可以代理您sildes[i]setTimeout函数回调、ref小提琴示例

$('document').ready(function () {
    $.ajax({
        type: "POST",
        url: "getSlides.php",
        data: '',
        cache: false,
        success: function (response) {
            var slides = JSON.parse(response);
            for (var i = 0; i < slides.length; i++) {
                setTimeout(function (slide) {
                    if (slide.type === 'image') {
                        $('#slideshow').html('<img src="' + slide.image_video + '" />');
                    }
                }, 2000, slides[i]);
            }
        }
    });
});
于 2013-11-14T11:39:58.273 回答
0

如果我没记错的话,这是一个经典错误,setTimeout 将在循环结束后执行异步,所以回调运行时 i 应该等于 slides.length。要解决此问题,请为 i 创建一个本地范围,如下所示:

                for (var i = 0; i < slides.length; i++) {
                    (function(i){
                      setTimeout(function(callback) {
                        if (slides[i].type === 'image') {
                            $('#slideshow').html('<img src="' + slides[i].image_video + '" />');
                        }
                      }, 2000);
                    })(i)
                }
于 2013-11-14T11:36:25.820 回答
0

because due to setTimeOut it calls your code after 2000ms, on that time value of i is slides.length for which it throw the error.

You have to arrange for a distinct copy of "i" to be present for each of the timeout functions.

 success: function (response) {
     var slides = JSON.parse(response);
     for (var i = 0; i < slides.length; i++) {
         doSetTimeout(slides, i);

     }
 }

 function doSetTimeout(slides, i) {
     setTimeout(function () {
         if (slides[i].type === 'image') {
             $('#slideshow').html('<img src="' + slides[i].image_video + '" />');
         }
     }, 2000);
 }
于 2013-11-14T11:41:42.877 回答
0

尝试替换:

for (var i = 0; i < slides.length; i++) {

至:

for (var i  in slides) {
于 2013-11-14T11:42:14.817 回答