Pausing a Jquery.each loop to ensure code is fully executed?
<p>Quick question - I've got a $.each loop, with a function call inside with a callback function</p>

<p>what can i put inside of that callback function to continue the loop</p>

<pre><code>$.each(product, function () {
    AddToCart($(this), function () {

    });
});
</code></pre>

<p>right now - if it loops through 5 products, only the last one gets added to the cart</p>

<p>i need a way of pausing the loop to ensure the product is added to the cart before adding the next one</p>

<p>thanks!</p>

<p>Here is the AddToCart function:</p>

<pre><code>function AddToCart(product, callback) {
            //Get the product ID and info
            var product = product;
            var productid = product.children(".productId").val();
            var productName = product.children(".productName").val();
            var productPage = product.children(".productPage").attr("href");
            var productImage = product.find(".productImage").attr("src");
            var productPrice = product.find(".productPrice").val();
            var productMSRP = product.find(".productMSRP").val();

            var productList = $("#productList");
            var exists = false;
            listItems = $("#productList").find("li");
            // Loop through to see if the product has already been added
            listItems.each(function (idx, li) {
                var item = $(li);
                var itemId = item.children(".productId").val();
                if (itemId == productid) {
                    exists = true;
                    //find its quantity
                    var quantity = item.children(".quantity").html();

                    //increment the quantity
                    quantity = parseInt(quantity) + 1;
                    item.children(".quantity").html(quantity);
                }
            });

            if (!exists) {
                var listItem = "&lt;li&gt;" +
                                    "&lt;a href='" + productPage + "'&gt;&lt;img height='80px' src='" + productImage + "'/&gt;&lt;/a&gt;" +
                                    "&lt;input type='button' value='X' class='cartRemoveBtn'/&gt;" +
                                    "&lt;span class='quantity'&gt;1&lt;/span&gt;" +
                                    "&lt;div class='tooltip_info'&gt;" +
                                    "&lt;span class='tooltip_name'&gt;" + productName + "&lt;/span&gt;" +
                                    "&lt;span class='tooltip_price'&gt;Our Price: $" + parseFloat(productPrice, 10).toFixed(2) + "&lt;/span&gt;" +
                                    "&lt;span class='tooltip_msrp'&gt;List Price: $" + parseFloat(productMSRP, 10).toFixed(2) + "&lt;/span&gt;" +
                                    "&lt;span class='tooltip_savings'&gt;You Save: $" + parseFloat(productMSRP - productPrice, 10).toFixed(2) + "&lt;/span&gt;" +
                                    "&lt;/div&gt;" +
                                    "&lt;input type='hidden' class='productId' value='" + productid + "' /&gt;" +
                                    "&lt;input type='hidden' class='productPrice' value='" + productPrice + "' /&gt;" +
                                    "&lt;input type='hidden' class='productMSRP' value='" + productMSRP + "' /&gt;" +
                               "&lt;/li&gt;";


                productList.prepend(listItem);
            }
            OpenCart();
            updateTotals();
            initializeCart();
            refreshTooltips();
            $.post('/Store/Cart/AddToCart/' + productid, function () {
                callback();
            });
            return false;
        }
</code></pre>
