Dos eventos onclick de Ajax

So I have had to modify some old existing code and add another ajax event to onclick para que tenga onclick="function1(); function2();"

This was working fine on our testing environment as it is a slow VM but on our live environment it causes some issues as function1() has to finished updating some records before function2() se llama.

Is there a good way to solve this without modifying the js for function2() as this the existing code which is called by other events.

Muchas Gracias

preguntado el 12 de junio de 14 a las 10:06

function2 won't run until function1 is finished. unless you use asyncronious ajax requests, in that case use callbacks. -

Etiquetaste tu pregunta con prototipo, Quieres decir prototipojs? -

Yep I do mean prototypejs -

Ok, I updated my answer with an example that runs with PrototypeJS -

is async: false, helpful for you? Just make the functions synchronize so function 2 will not execute unless function 1 returns your expected value. -

4 Respuestas

Call function2 upon returning from function1:

function function1() {

$.ajax({
    type: "POST",
    url: "urlGoesHere",
    data: " ",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (result) {
        //call function2

    },
    error: 
});

}

Or wrap them in a function that calls both 1 and 2.

Respondido el 12 de junio de 14 a las 10:06

You need to use always callback of ajax method, check out always callback of $.ajax() method http://api.jquery.com/jquery.ajax/.

The callback given to opiton is executed when the ajax request finishes. Here is a suggestion :


function function1() {

    var jqxhr = $.ajax({
        type: "POST",
        url: "/some/page",
        data: " ",
        dataType: "dataType",
    }).always(function (jqXHR, textStatus) {
        if (textStatus == 'success') {
            function2();
        } else {
            errorCallback(jqXHR);
        }
    });

}

Respondido el 12 de junio de 14 a las 10:06

I'm assuming you use Prototype JS and AJAX because of your tags. You should use a callback function:

function function1(callback) {
    new Ajax.Request('http://www.google.nl', {
        onSuccess: function(response) {
            callback();
        }
    });
}

function function2(callback) {
    new Ajax.Request('http://www.google.nl', {
        onSuccess: function(response) {
            callback();
        }
    });
}

function both() {
    function1(function() {
        function2();
    });
}

Entonces usa onclick="both();" on your html element.

Ejemplo: http://jsfiddle.net/EzU4p/

Respondido el 12 de junio de 14 a las 15:06

I don't think the way you mentioned is valid. please test it once. - SR verde

You were right, there were lots of errors in my code. jsfiddle was down yesterday. I included a working example now. - TS

Ajax has async property which can be set false. This way, you can wait for that function to complete it's call and set some value. It actually defeats the purpose of AJAX but it may save your day.

I recently had similar issues and somehow calling function2 after completing function1 worked perfectly. My initial efforts to call function2 on function1 success didn't work.

      $.ajax({
                type: "POST",
                url: "default.aspx/function1",
                data: "",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                async: false, // to make function Sync
                success: function (msg) {
                    var $data = msg.d;
                    if ($data == 1)
                    {
                        isSuccess = 'yes'
                    }
                },

                error: function () {
                    alert('Error in function1');
                }
            });

            // END OF AJAX

           if (isSuccess == 'yes') {
                // Call function 2
           }

Respondido el 14 de junio de 14 a las 19:06

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.