ajax三个函数调用,JQuery / Ajax - 完成多个JSON / GET请求时调用函数

作者寻求解决在使用递归的Ajax GET请求获取数据时,如何确保在所有数据加载完成后调用tuneTileLoadedFunction()的问题,可能涉及jQuery.when的使用但未奏效。最终解决方案是通过检查每个项目是否达到目标条件而非等待每个请求完成。
摘要由CSDN通过智能技术生成

我正在制作多个JSON GET,直到满足条件。下面的代码的问题是在满足条件之前(在最终的GET请求之前)调用函数tuneTileLoadedFunction()。我只想在最终加载完成后调用该函数。

我该怎么做?是否与http://api.jquery.com/jquery.when/有关?我已经看过这个了,但是就我已经做过的事情而言,它无法理解它。

当我放置tuneTileLoadedFunction()代替//它不会调用该函数时(我猜因为数据尚未加载)。

谢谢:)

checkIfTuneTileLoaded();

function checkIfTuneTileLoaded() {

var tuneTileLoaded = $("a[href*='" + location.pathname + "']").length;

alert(tuneTileLoaded);

if (tuneTileLoaded == 1) {

//whatever

} else {

alert('tuneTileNotPresent');

tumblrPostsRetrieved += 20;

$.ajax({

type: 'GET',

url: 'http://api.tumblr.com/v2/blog/onrepeatthisweek.tumblr.com/posts?api_key=cCaCiEE6kd5pInej2YFU0xdC4msLOE3R3IhYXcR1W6Irza8sJq&tag=overandoverandoverandover&offset=' + tumblrPostsRetrieved,

data: { get_param: 'value' },

dataType: 'jsonp',

success: function (data) {

for (i = 0; i <= 19; i++) {

$('#recent-posts').append('

' + data.response.posts[i].title + '');

}

maxScrollLeft = document.getElementById("tunesID").scrollWidth - document.getElementById("tunesID").clientWidth;

checkIfTuneTileLoaded();

tuneTileLoadedFunction();

alert('success');

}

});

}

}

更新/决议:

我没有解决我试图解决的问题(在其中监听带有Ajax请求的递归函数的结束)。我的解决方案不是等待每个请求完成,而是检查每个项目,因为它们是为我所寻找的内容而呈现的:

for (i = 0; i <= 19; i++) {

$('#recent-posts').append('

' + data.response.posts[i].title + '');

if (data.response.posts[i].post_url == window.location) {

tuneTileLoaded = 1;

tuneTileLoadedFunction();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值