Javascript: trigger action on function exit

Is there any way to listen for javascript function to exit? A trigger that can be configured at the end of a function?

I am trying to use the user interface obfuscation method (BlockUI) while the AJAX object retrieves data from the database, but the function is not necessarily executed last, even if you put it at the end of the function call.

Example:

function doStuff() {
  blockUI();
  ajaxCall();
  unblockUI();
};

Is there a way to make doStuff to listen to ajaxCall before running unBlockUI? Be that as it may, it processes the function linearly, calling each object in order, then a separate thread is generated to complete each of them. Thus, although my AJAX call may take 10-15 seconds, I block the user for only a split second due to the linear execution of the function.

There are less elegant ways around this ... to end the loop only when the return value set by the AJAX function is set to true or something like that. But it seems unnecessarily complicated and inefficient.

+3
source share
6 answers

However, you are executing your Ajax routines, you need a β€œcallback” function that will run after it finishes:

function ajaxCall(callback){
    //do ajax stuff...
    callback();
}

Then:

function doStuff(){
    blockUI();
    ajaxCall(unblockUI);
}
+4
source

Your AJAX call must have a callback function specified. You can call unblockUI from the callback.

SAJAX is a simple AJAX library that helps you make AJAX calls more.

There is also another post that describes what you are looking for.

+1

xhr. ( , ).

0

, , , , . , Prototype JQuery ... .

0

, unblockUI(), ajax , jQuery, :

function doStuff(){

    blockUI();

    jQuery.ajax({
        url: "example.com",
        type: "POST",           //you can use GET or POST
        success: function(){

            unblockUI();
        }
    });
}
0

It seems to me that you want the user to wait while the information is retrieved from db. What I do when I make an Ajax call for some information from the database is to display an animated gif that says "get it ..." - it flashes constantly until the information is restored and displayed on the web page . When the information is displayed, the animated gif is disabled / hidden, and the focus moves to the new information displayed. An animated gif allows the user to know that something is happening.

0
source

Source: https://habr.com/ru/post/1697529/


All Articles