SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Wizard
    Join Date
    Mar 2002
    Location
    Bristol, UK
    Posts
    2,240
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    JQuery code executing multiple times

    Hi everyone,

    I'm using the following code to make a simple database update via a JQuery $.get call:

    Code JavaScript:
    function updateItem(obj) {
                obj.css('background-image','url(/image/frontend/flag.png)');
                setTimeout("obj.parents('li').hide('blind',{},'fast');",2500);
            }
     
    $(".flag").click(function() {
     
                obj = $(this);
     
                if(confirm('Are you sure you want to flag this item? This will remove the item from the site, pending moderator review.')) {
     
                    $.get("/include/frontend/flag.php", {
                        id: $(this).attr("rel")
                    },
                    function() {
                        updateItem(obj);
                    });
     
                }
                return false;
            });

    The code itself works fine in that it does what it's supposed to, but it often executes multiple times - clicking once on an link with the flag class assigned to it can make this code execute up to four times!

    Does anyone know why it's deciding to do this?

  2. #2
    SitePoint Enthusiast stakes's Avatar
    Join Date
    Sep 2008
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried your code, works fine for me, what browser are you using? What functions does the multiple execution? Does your script produce four ajax calls?
    webcloud.se - Articles on web development

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2002
    Location
    Bristol, UK
    Posts
    2,240
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yep, the Ajax call executes four times and everything. I probably wouldn't have noticed this had it not been for the confirm() box, which popped up for each execution of the code. I'm going to keep my thinking cap on tonight but if anyone has any thoughts I'd love to hear them.

  4. #4
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SJH View Post
    Yep, the Ajax call executes four times and everything. I probably wouldn't have noticed this had it not been for the confirm() box, which popped up for each execution of the code. I'm going to keep my thinking cap on tonight but if anyone has any thoughts I'd love to hear them.
    Hi.
    Also for me works fine.
    I didn't get the trouble.

    I tried it with:
    PHP Code:
    <ul>
    <
    li><a href="#" class="flag" rel="1">one</a></li>
    <
    li><a href="#" class="flag" rel="2">two</a></li>
    <
    li><a href="#" class="flag" rel="3">three</a></li>
    </
    ul
    If I clicked on all the links
    in Firebug I've got three requests


    Bye.

  5. #5
    SitePoint Enthusiast stakes's Avatar
    Join Date
    Sep 2008
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you could show us the full context it might help, are you wrapping the code in a plugin for each statment or something?
    webcloud.se - Articles on web development


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •