Wait until all ajax requests are done

前端 未结 4 512
半阙折子戏
半阙折子戏 2020-12-18 12:11

I need to wait until all my ajax functions are done, and then continue the exectution.

My particular case is that I need to translate some fields in a form before su

4条回答
  •  失恋的感觉
    2020-12-18 12:37

    You can use callback

    function translate(..., callback) {
        $.ajax({
            ...
            success:function(data) {
                ...
                callback(data);
            }
        });
    };
    

    And pass your after ajax code to it

    $("#form").submit(function() {
        translatable_fields.each(function() {
            translate(..., function(result){
    
                if (!(this).hasClass('ready')) {
                    $.ajax({
                        //validation
                        success: function(data) {
                            if (data['isValid']) {
                                $("#form").addClass('ready');
                                $("#form").submit();
                            }
                        }
                    });
                }
                return true;
            });
        });
    });
    

提交回复
热议问题