我在将参数传递给匿名函数时遇到问题。图像是一个文件名数组。我确定它是在我尝试传递它之前定义的,并且它在下面的匿名函数中是未定义的。这是怎么回事?我为任何缺乏细节而道歉。使用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);
}
});
}
答案 0 :(得分: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);
}
});
}
答案 1 :(得分: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回调中访问