0

我无法将参数传递给匿名函数。图像是一个文件名数组。我确定它是在我尝试传递它之前定义的,并且它在下面的匿名函数中是未定义的。这是怎么回事?对于任何缺乏细节,我深表歉意。使用 jQuery 1.9

function setup_slideshow(){
$('#slide1').fadeOut(0);
$('#slide0').fadeIn(0);
$.ajax({
    url: "/inc/phplib/fetch_images.php",
    async: true,

    // Receives a string with a colon separated list of filenames (images)
    complete: function(data){
        var images = data.responseText.split(":");
        $('#slide0').attr("src", images[0]);
        $('#slide1').attr("src", images[1]);
        var t = setTimeout(function(images){
            move_slideshow(images, 2, 1);
        }, 550);
    }
});
}
4

2 回答 2

0

你写了// Receives a string with a comma separated list of filenames (images)

但是你用的var images = data.responseText.split(":");应该是var images = data.responseText.split(",");

完整代码

function setup_slideshow(){
  $('#slide1').fadeOut(0);
  $('#slide0').fadeIn(0);
  $.ajax({
    url: "/inc/phplib/fetch_images.php",
    async: true,

    // Receives a string with a comma separated list of filenames (images)
    success: function(data){
        alert(data); //to check what you get in data
        var images = data.split(":");
        $('#slide0').attr("src", images[0]);
        $('#slide1').attr("src", images[1]);
        var t = setTimeout(function(images){
            move_slideshow(images, 2, 1);
        }, 550);
    }
  });
}
于 2013-03-07T06:18:46.310 回答
0

尝试

   var images = data.responseText.split(":");
    $('#slide0').attr("src", images[0]);
    $('#slide1').attr("src", images[1]);
    var t = setTimeout(function(){
        move_slideshow(images, 2, 1);
    }, 550);

您不需要将图像数组传递给 setTimeout,因为它在更高的范围内,并且可以在 setTimeout 回调中访问

于 2013-03-07T06:19:45.100 回答