prevent a function running multiple times after calling it again from ajax

Albert Kuzmin

I have been struggling to find a good solution for this problem for a long time. (And I have GOOGLED IT many times). I'm trying to avoid the same function running multiple times when I recall it after an ajax call. I'll give a simple example using the following code:

I load a list of users from my db using php:

while(some loop){
    $users.='
        <div>
            <div>user name</div>
            <div class="replace">replace user</div>
            <div class="delete">delete user</div>
        </div>
    ';
}

And display them inside a div:

<div class="user_wrap">
    <?php echo $users; ?>
    <div id="add_user"></div>
</div>

Now on the front end I can delete or replace each user or add a new user to the div so I run all three functions on document ready:

$(document).ready(function(){
    addUser();
    deleteUser();
    replaceUser();
});

function addUser(){
    $("#add_user").on("click",function(){
        //first load a list of users then select one from it
        $.post("url",{},function(response){
            //add new user to the user wrap
            deleteUser();
            replaceUser();
        });
    });
}

function deleteUser(){
    $(".delete").on("click",function(){
        $.post("url",{},function(response){
            //delete user
        });
    });
}

function replaceUser(){
    $(".replace").on("click",function(){
        $.post("url",{},function(response){
            //replace user with a different user
        });
    });
}

Now the problem comes when I add a new user using ajax I have to rerun both of the deleteUser and replaceUser functions. So thoes functions begin to pile up and can run multiple times in a row. In some cases this is harmless to the result of the function but in some cases it actually harms the result of the function and in adition doesnt it take a lot of system resourses? The only way I was able to prevent it was using Triggers by setting up a global variable and doing some thing like:

DelteUserTrigger = 0;

function deleteUser(){
    if(DelteUserTrigger==1){return false;}
    else{
        DelteUserTrigger=1;
        $(".delete").on("click",function(){
            $.post("url",{},function(response){
                DelteUserTrigger==0;
                //delete user
            });
        });
    }
}

But this mehtod doesnt work as expected it gets the function "stuck" many times as if the trigger didnt change, the on click event just stops responding.

I have also read something about function closure in JS to prevent the same function from running multiple times but I didnt quit understand how to implement that with the ajax call. And is it really useful in this case or am I doing everything wrong here? I would really appreciate some help here.

Pointy

Just use event delegation, and you'll only have to set up the event handlers once (when the page loads):

function deleteUser(){
    $(document).on("click", ".delete", function(){
        $.post("url",{},function(response){
            //delete user
        });
    });
}

Once that's been called, then it'll work for all the user stanzas whether they were there when the page loaded or not. You don't need to do anything after the ajax completes.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

prevent calling ajax multiple times

From Dev

how to prevent calling a function multiple times in Jquery

From Dev

Prevent js function running multiple times

From Dev

Prevent js function running multiple times

From Dev

Calling to ajax multiple times from javascript

From Dev

Javascript - prevent function from executing multiple times

From Dev

Prevent $rootScope.$on from calling function several times

From Dev

How to prevent a View from loading multiple times after the first call?

From Dev

Calling a jQuery function multiple times

From Dev

Calling onmouseover function multiple times

From Dev

Jquery: how to prevent running multiple times?

From Dev

Calling startService() multiple times on a running service

From Dev

Calling startService() multiple times on a running service

From Dev

Running a function multiple times in R

From Dev

Jquery Function running multiple times

From Dev

EventListener click function firing multiple times after initialize again during infinite scroll

From Dev

Reusing AJAX function multiple times

From Dev

How can I prevent multiple ajax queries from being run on "prev" button being clicked multiple times

From Dev

How can I prevent multiple ajax queries from being run on "prev" button being clicked multiple times

From Dev

In Java, how do I prevent Graphics Applets from running multiple times?

From Dev

How can I prevent my Ajax function to fire multiple times on click?

From Dev

Avoid numbers incrementing multiple times when calling a function multiple times

From Dev

AngularJs prevent evaluating function multiple times

From Dev

Why printing multiple times on calling a function - AngularJS

From Dev

Calling the same function multiple times in select

From Dev

Calling the same function multiple times in select

From Dev

Click event is calling multiple times within function

From Dev

Preventing xmlhttp function from running again on keyup

From Dev

Prevent Ad from triggering multiple times

Related Related

  1. 1

    prevent calling ajax multiple times

  2. 2

    how to prevent calling a function multiple times in Jquery

  3. 3

    Prevent js function running multiple times

  4. 4

    Prevent js function running multiple times

  5. 5

    Calling to ajax multiple times from javascript

  6. 6

    Javascript - prevent function from executing multiple times

  7. 7

    Prevent $rootScope.$on from calling function several times

  8. 8

    How to prevent a View from loading multiple times after the first call?

  9. 9

    Calling a jQuery function multiple times

  10. 10

    Calling onmouseover function multiple times

  11. 11

    Jquery: how to prevent running multiple times?

  12. 12

    Calling startService() multiple times on a running service

  13. 13

    Calling startService() multiple times on a running service

  14. 14

    Running a function multiple times in R

  15. 15

    Jquery Function running multiple times

  16. 16

    EventListener click function firing multiple times after initialize again during infinite scroll

  17. 17

    Reusing AJAX function multiple times

  18. 18

    How can I prevent multiple ajax queries from being run on "prev" button being clicked multiple times

  19. 19

    How can I prevent multiple ajax queries from being run on "prev" button being clicked multiple times

  20. 20

    In Java, how do I prevent Graphics Applets from running multiple times?

  21. 21

    How can I prevent my Ajax function to fire multiple times on click?

  22. 22

    Avoid numbers incrementing multiple times when calling a function multiple times

  23. 23

    AngularJs prevent evaluating function multiple times

  24. 24

    Why printing multiple times on calling a function - AngularJS

  25. 25

    Calling the same function multiple times in select

  26. 26

    Calling the same function multiple times in select

  27. 27

    Click event is calling multiple times within function

  28. 28

    Preventing xmlhttp function from running again on keyup

  29. 29

    Prevent Ad from triggering multiple times

HotTag

Archive