在另一个函数完成JavaScript和JQuery后运行函数
我需要一点帮助。我正在尝试运行第二个函数“likeLinks();”,但只有在第一个函数“getLikeURLs();”完成之后。这是因为我的第二个函数依赖于links数组来执行。看起来他们想同时跑 任何帮助都将不胜感激在另一个函数完成JavaScript和JQuery后运行函数,javascript,jquery,function,Javascript,Jquery,Function,我需要一点帮助。我正在尝试运行第二个函数“likeLinks();”,但只有在第一个函数“getLikeURLs();”完成之后。这是因为我的第二个函数依赖于links数组来执行。看起来他们想同时跑 任何帮助都将不胜感激 var links = []; var url = '/' + window.location.pathname.split('/')[1] + '/' + window.location.pathname.split('/')[2] + '/' get
var links = [];
var url = '/' + window.location.pathname.split('/')[1] + '/' + window.location.pathname.split('/')[2] + '/'
getLikeURLs();
likeLinks();
function getLikeURLs() {
for (i = 1; i < parseInt(document.getElementsByClassName('PageNav')[0].getAttribute('data-last')) + 2; i++) {
var link = $.get(url + 'page-' + i, function(data) {
//gets the like links from current page
$(data).find('a[class="LikeLink item control like"]').each(function() {
links.push($(this).attr('href')); // Puts the links in the Array
});
});
}
}
function likeLinks() {
for (t = 0; t <= links.length; t++) {
var token = document.getElementsByName('_xfToken')[0].getAttribute('value')
$.post(links[t], {
_xfToken: token,
_xfNoRedirect: 1,
_xfResponseType: 'json'
}, function(data) {});
}
}
var-links=[];
var url='/'+window.location.pathname.split('/')[1]+'/'+window.location.pathname.split('/')[2]+'/'
getLikeURLs();
likeLinks();
函数getLikeURLs(){
对于(i=1;i 对于(t=0;t编辑:我的答案讨论了这个问题,但请参阅Alnitak答案以获得更好的解决方案。
get-in-getLikeURLs和put-in-LikeLink都是异步的。对这两个函数的调用都会立即返回。当数据在不确定的时间之后从被调用的服务器返回时,回调函数就会被调用。put可能会在get之前返回,这在您的情况下是一个问题。还请注意,Java脚本不是多线程的,因此getLikeURLs和LikeLink这两个方法永远不会同时运行。另一方面,回调函数可能会在以后的任何时候调用,而不保证回调顺序。例如,第三个get/put可能会在循环中第一个get/put之前返回
您可以使用$.ajax指定get和put是同步的,但这是不明智的,因为如果任何get/put没有在合理的时间内返回(例如,服务器处于脱机状态),浏览器将挂起。此外,您没有“多任务”功能发送大量请求并让不同的服务器同时工作的好处是,它们可以连续地工作
诀窍是简单地从getLikeURL中的回调函数调用LikeLink。由于for循环,您的情况有点棘手,但这应该可以工作:
var links = [];
var url = '/' + window.location.pathname.split('/')[1] + '/' + window.location.pathname.split('/')[2] + '/'
getLikeURLs();
//likeLinks(); // Don't call yet. Wait for gets to all return.
function getLikeURLs() {
var returnCount = 0; // Initialize a callback counter.
var count = parseInt(document.getElementsByClassName('PageNav')[0].getAttribute('data-last')) + 1;
for (i = 0; i < count; i++) {
var link = $.get(url + 'page-' + (i + 1), function(data) {
//gets the like links from current page
$(data).find('a[class="LikeLink item control like"]').each(function() {
links.push($(this).attr('href')); // Puts the links in the Array
});
// If all gets have returned, call likeLinks.
returnCount++;
if (returnCount === count) {
likeLinks();
}
});
}
}
function likeLinks() {
for (t = 0; t <= links.length; t++) {
var token = document.getElementsByName('_xfToken')[0].getAttribute('value')
$.post(links[t], {
_xfToken: token,
_xfNoRedirect: 1,
_xfResponseType: 'json'
}, function(data) {});
}
}
var-links=[];
var url='/'+window.location.pathname.split('/')[1]+'/'+window.location.pathname.split('/')[2]+'/'
getLikeURLs();
//likeLinks();//暂时不要调用。等待gets返回。
函数getLikeURLs(){
var returnCount=0;//初始化回调计数器。
var count=parseInt(document.getElementsByClassName('PageNav')[0].getAttribute('data-last'))+1;
对于(i=0;i 对于(t=0;t而言,链接
变量实际上是-将它们存储在一个数组中,然后您可以使用$。when()
创建一个新的延迟对象,该对象仅在所有先前的$操作完成后才会解析。get()
操作完成时:
function getLikeURLs(url) { // NB: parameter, not global
var defs = [], links = []; // NB: links no longer global
for (...) {
var link = $.get(...);
defs.push(link);
}
// wait for previous `$.get` to finish, and when they have create a new
// deferred object that will return the entire array of links
return $.when.apply($, defs).then(function() { return links; });
}
然后,启动功能链:
getLikeURLs(url).then(likeLinks);
请注意,likeLinks
现在将传递链接数组,而不是从全局状态访问它。还应重写该函数以允许您等待其$。post
调用也将完成:
function likeLinks(links) {
// loop invariant - take it outside the loop
var token = document.getElementsByName('_xfToken')[0].getAttribute('value');
// create array of deferreds, one for each link
var defs = links.map(function(link) {
return $.post(link, {
_xfToken: token,
_xfNoRedirect: 1,
_xfResponseType: 'json'
});
});
// and another for when they're all done
return $.when.apply($, defs);
}
p、 不要把那个(相对)昂贵的parseInt(document.getAttribute(…)
for
语句中的表达式-它将导致每次迭代都对其求值。在循环外计算一次,并将其存储在变量中。还有一些不必要重复调用的地方,例如window.location.pathname.split()
将ajax get和post方法调用为异步:false@abc123是的!再次感谢。:)@abc123不,不,不!不要使用async:false
,这是这个场景中最糟糕的建议!@Alnitak我也有过类似的情况,我使用这个async:false,我知道有性能问题,我想用更好的替代品来改变它。如果可以的话,请为这个场景推荐任何可能的替代品。这将我会帮上大忙的。这是(IMHO)jQuery 1.5之前的天真方法。只需使用$。当使用时,就可以了!不过解释得很好,Mark.+1来自我,-1来自我,因为它(不必要地)将getLikeURLs()中对likeLinks
的调用紧密结合在一起
功能。谢谢。如果您需要Oneplus One邀请,请在Oneplus论坛上发送PM@kallen。