There is a showItems function that receives all products from the database and inserts them into the shopping page in the form of consecutive pictures with a description, there is a second function checkMyItems, which receives from the database id the goods that the user selected. Products are represented by a div element with a karinka on the background. The problem is that once the second function runs out faster than the first one, everything starts to break down visually. How to make so that we were sure that the first script was completely worked out and loaded all the pictures?

I tried it this way, but the problem remains:

$.when(showItems()).then(checkMyItems()); function showItems(){ $.ajax({ url: "shop.php", data: { Action: "getItems" }, type: 'post', success: function(data) { $.each(data.items, function(i, items) { //вставляем items ... }); } 
  • What does the showItems function return? - Grundy
  • can part of the showItems function? - Stanislav Grot
  • The function returns nothing, it simply receives data on ajax and inserts through each in the right places - Kula Hula
  • Add it to the question - Grundy

1 answer 1

$.ajax returns a deffered object that has a then function. Therefore, in this case, no when needed, it is enough to return the result of $.ajax execution from the showItems function.

 function showItems(){ return $.ajax(...) } 

Next, add a handler through the function then

 showItems().then(checkMyItems); 
  • Thank! Works! - Kula Hula Nov.