我如何在每个项目和每个项目的fadeIn之间延迟迭代以下foreach?
我不确定.append()是最好用的函数,因为我想要实现的是为json中的每个人加载带有#fan类的模板化div,如下所示: / p>
[{"first_name":"adf","location":"agd","image_path":""},{"first_name":"test","location":"test","image_path":""},{"first_name":"chris","location":"london","image_path":""}]
到目前为止,这是我的ajax调用代码。
function get_fans(){
$.ajax('http://localhost/facebook_app/selectFans/', {
type : 'get',
dataType: 'json',
success : function (data) {
$.each(data, function(index) {
$('section#fans').append('<div class="fan">' + data[index].first_name + ', ' + data[index].location +'</div>');
});
},
error: function () {
$('section#fans').text('Could not load fans.');
}
});
}
答案 0 :(得分:1)
这应该有效:
$.each(data, function(index) {
setTimeout(function(){
var d = $('<div class="fan">' + data[index].first_name + ', ' + data[index].location +'</div>');
$('section#fans').append(d);
d.hide();
d.fadeIn();
}, 1000*(index +1));
});