Wait for AJAX before continuing through separate function

前端 未结 2 1938
离开以前
离开以前 2021-02-05 21:47

Alright... at 2am, this is where I draw the line. Help... before my laptop ends up going out the window. :)

I\'ve tried using setTimer, callbacks, and everything else I

2条回答
  •  情深已故
    2021-02-05 22:35

    Do not use async: false option. It's a pure evil (blocks all scripts in browser and even other tabs!) and it's deprecated since jQuery 1.8. You should use callbacks as it was always meant to be.

    function parseRow(row) {
        /* the other code */
        autoSelectCategory(payee, function() {
            saveRecord(date, checkNum, payee, memo, category, payment, deposit);
        });
    }
    
    function autoSelectCategory(payee, callback) { // <---- note the additional arg
        $.ajax({
            url: "autoselectcategory",
            dataType: "json",
            data: {
                string: payee
            },
            success: function(res) {
                /* the other code */
                callback();
            }
        });
    }
    

提交回复
热议问题