Note that there are some explanatory texts on larger screens.

plurals
  1. POCallback on function call in $.each
    text
    copied!<p>I call a function in an $.each loop, but i want to make sure, that the next iteration is not called until the previous function is done.</p> <pre><code>$(".btn_loadfavorites").on("click",function(event){ $.getJSON('utility/get_user_compare_pages.php', {uid: user}, function(rsp) { $.each(rsp, function(i, favorite_pageid) { loadNewFacebookPage(favorite_pageid); }); }); }); </code></pre> <p>Here is the function (I stripped it of the unimportant stuff):</p> <pre><code>function loadNewFacebookPage(newfbpage){ if (isUrl(newfbpage) || newfbpage.substr(0,3) == "www"){ newfbpage = newfbpage.substr(newfbpage.lastIndexOf('/') + 1); } $.getJSON( "https://graph.facebook.com/"+newfbpage+"?fields=picture,name,category,likes,talking_about_count,link,website" ) .done(function( rsp ) { $('#loadmodal').modal('show'); var newpageid = rsp.id; $("*[data-pcpageid]").each(function(i, elm) { if(newpageid == $(this).data("pcpageid")){ pageexists = true; alert('Page has already been added'); return; } }); if(pageexists == false){ pagename = rsp.name.split(" "); pagepicture = rsp.picture.data.url; $('.grid_fbpages').append("&lt;li class='grid_li_fbpages' data-pcpageid='"+newpageid+"' style='max-width:20%;'&gt;&lt;img src='"+pagepicture+"' class='img-circle' style='display:inline;margin-right:15px;'&gt;&lt;h4 style='display:inline;'&gt;"+pagename[0]+"&lt;/h4&gt;&lt;a href='javascript:void(0)' class='btn_removefbpage' style='float:right' data-pcpageid='"+newpageid+"'&gt;&amp;#10005&lt;/a&gt; &lt;a href='javascript:void(0)' class='btn_addtofavorites' style='float:right' data-pcpageid='"+newpageid+"'&gt;&amp;#9733;&lt;/a&gt;&lt;hr&gt;Likes: "+rsp.likes+"&lt;br&gt;PTAT: "+rsp.talking_about_count+"&lt;br&gt;&lt;br&gt;&lt;/li&gt;"); //GET POSTS $.getJSON('utility/get_compare_posts.php', {access_token: access_token, pid: newpageid}, function(rsp) { $.each(rsp, function(postId, data) { //TOP POSTS if (data.hasOwnProperty('likes')){ top_posts_likes.push(data.likes.like_count); if (data.hasOwnProperty('comments')){ top_posts_comments.push(data.comments.comment_count); }else{ top_posts_comments.push('0'); } top_posts_message.push(data.message); top_posts_id.push(data.postId); } }); //TOP POSTS $(".grid_topposts").append("&lt;li data-pcpageid='"+newpageid+"' style='max-width:20%;text-align:left;'&gt;&lt;img src='"+pagepicture+"' class='img-circle' style='display:inline;margin-right:15px;'&gt;&lt;h4 style='display:inline;'&gt;"+pagename[0]+"&lt;/h4&gt;&lt;/li&gt;"); most_popular_post_index = top_posts_likes.indexOf(Math.max.apply(Math, top_posts_likes)); $.getJSON( "https://graph.facebook.com/"+top_posts_id[most_popular_post_index]+"?fields=picture&amp;access_token="+access_token+"", function(rsp) { $(".grid_topposts").append("&lt;li data-pcpageid='"+newpageid+"' style='max-width:20%;'&gt;&lt;img src='"+rsp.picture+"'&gt;&lt;br&gt;"+top_posts_message[most_popular_post_index]+"&lt;br&gt;Likes: "+top_posts_likes[most_popular_post_index]+" Comments: "+top_posts_comments[most_popular_post_index]+"&lt;/li&gt;"); top_posts_likes.splice(most_popular_post_index,1); top_posts_message.splice(most_popular_post_index,1); top_posts_id.splice(most_popular_post_index,1); top_posts_comments.splice(most_popular_post_index,1); most_popular_post_index = top_posts_likes.indexOf(Math.max.apply(Math, top_posts_likes)); $.getJSON( "https://graph.facebook.com/"+top_posts_id[most_popular_post_index]+"?fields=picture&amp;access_token="+access_token+"", function(rsp) { $(".grid_topposts").append("&lt;li data-pcpageid='"+newpageid+"' style='max-width:20%;'&gt;&lt;img src='"+rsp.picture+"'&gt;&lt;br&gt;"+top_posts_message[most_popular_post_index]+"&lt;br&gt;Likes: "+top_posts_likes[most_popular_post_index]+" Comments: "+top_posts_comments[most_popular_post_index]+"&lt;/li&gt;"); top_posts_likes.splice(most_popular_post_index,1); top_posts_message.splice(most_popular_post_index,1); top_posts_id.splice(most_popular_post_index,1); top_posts_comments.splice(most_popular_post_index,1); most_popular_post_index = top_posts_likes.indexOf(Math.max.apply(Math, top_posts_likes)); $.getJSON( "https://graph.facebook.com/"+top_posts_id[most_popular_post_index]+"?fields=picture&amp;access_token="+access_token+"", function(rsp) { $(".grid_topposts").append("&lt;li data-pcpageid='"+newpageid+"' style='max-width:20%;'&gt;&lt;img src='"+rsp.picture+"'&gt;&lt;br&gt;"+top_posts_message[most_popular_post_index]+"&lt;br&gt;Likes: "+top_posts_likes[most_popular_post_index]+" Comments: "+top_posts_comments[most_popular_post_index]+"&lt;/li&gt;"); top_posts_likes.splice(most_popular_post_index,1); top_posts_message.splice(most_popular_post_index,1); top_posts_id.splice(most_popular_post_index,1); top_posts_comments.splice(most_popular_post_index,1); }); }); }); //END TOP POSTS }); } }) .fail(function( error ) { alert('Did not find any match - Please try again with another name, ID or URL'); }); } </code></pre> <p>Thanks</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload