On click myFunction and wait until it is finished

caramba

I thought this is something easy to do but I dont find anything helping me out of this.

I have a function

(function($){
    myFunction = function(e){
        e.preventDefault();
        // do stuff
        // load ajax content
        // animate and show
    }
    $('.button').on( 'click', myFunction);
})(jQuery);

now this works but I need to know, wait untill everything is done if someone presses many .buttons in a short time cause there are a few elements with class button

I've tried with promise()

$('.button').on( 'click', function(){
    $.when( myFunction() ).done(function() {
        alert('finished')
    });
});

but that gives me an error e is undefined and

$('.button').on( 'click', myFunction).promise().done(function() {
    alert('finisehd');
});

anyone knowing what I'm doing wrong and how I could do it to get it to work?

mritz_p

The most common solution would be to set a variable inside the click handler when myFunction is called and check its state with every call of the click handler. This could be done somewhere along the lines of this:

(function($){
    var wait = false;

    myFunction = function(e){
        e.preventDefault();

        if (wait) {
            return;
        }

        wait = true;

        // ...

        wait = false;
    }
    $('.button').on( 'click', myFunction);
})(jQuery);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Wait until function is finished

From Dev

Wait until forEach loop is finished?

From Dev

Wait until gobalEval has finished

From Dev

Wait until NSURLConnection sendAsynchronousRequest is finished

From Dev

Android AsyncTask wait until finished

From Dev

Wait until my thread is finished

From Dev

Wait until gobalEval has finished

From Dev

Wait until process is finished running

From Dev

How can I force this jquery to wait until click event has finished?

From Java

How to wait until all async calls are finished

From Dev

Sequelize wait until loop finished with callback

From Dev

How to wait until an animation is finished in Swift?

From Dev

How to wait until all NSOperations is finished?

From Dev

wait until completion block not finished in AFJSONRPCClient request

From Dev

how to wait until a web request with HttpWebRequest is finished?

From Dev

How to wait until some jQuery methods are finished?

From Dev

How to wait until networking by Alamofire has finished?

From Dev

How to wait until my batch file is finished

From Dev

jQuery mobile: Wait until $.getJSON is finished

From Dev

looping for jquery load and wait until finished

From Dev

wait until completion block not finished in AFJSONRPCClient request

From Dev

Make Excel VBA wait until operation is finished

From Dev

How to get jQuery to wait until an animation is finished?

From Dev

Android - How to wait until a SnapshotListener has finished?

From Dev

How to wait until all tasks are finished before running code

From Dev

Why the main does not wait until the async method has finished?

From Dev

How to wait until all tasks finished without blocking UI thread?

From Dev

ExecutorService's shutdown() doesn't wait until all threads will be finished

From Dev

Run thread from Tkinter and wait until it's finished

Related Related

  1. 1

    Wait until function is finished

  2. 2

    Wait until forEach loop is finished?

  3. 3

    Wait until gobalEval has finished

  4. 4

    Wait until NSURLConnection sendAsynchronousRequest is finished

  5. 5

    Android AsyncTask wait until finished

  6. 6

    Wait until my thread is finished

  7. 7

    Wait until gobalEval has finished

  8. 8

    Wait until process is finished running

  9. 9

    How can I force this jquery to wait until click event has finished?

  10. 10

    How to wait until all async calls are finished

  11. 11

    Sequelize wait until loop finished with callback

  12. 12

    How to wait until an animation is finished in Swift?

  13. 13

    How to wait until all NSOperations is finished?

  14. 14

    wait until completion block not finished in AFJSONRPCClient request

  15. 15

    how to wait until a web request with HttpWebRequest is finished?

  16. 16

    How to wait until some jQuery methods are finished?

  17. 17

    How to wait until networking by Alamofire has finished?

  18. 18

    How to wait until my batch file is finished

  19. 19

    jQuery mobile: Wait until $.getJSON is finished

  20. 20

    looping for jquery load and wait until finished

  21. 21

    wait until completion block not finished in AFJSONRPCClient request

  22. 22

    Make Excel VBA wait until operation is finished

  23. 23

    How to get jQuery to wait until an animation is finished?

  24. 24

    Android - How to wait until a SnapshotListener has finished?

  25. 25

    How to wait until all tasks are finished before running code

  26. 26

    Why the main does not wait until the async method has finished?

  27. 27

    How to wait until all tasks finished without blocking UI thread?

  28. 28

    ExecutorService's shutdown() doesn't wait until all threads will be finished

  29. 29

    Run thread from Tkinter and wait until it's finished

HotTag

Archive