0

I have a weird problem that I am not able to debug.

I have a script with the following relevant functions:

  • a function events() that has a jQuery.on('click', callback(event)) event binder inside it. That function gets called every time I add or remove anchors with AJAX, but that's irrelevant for this problem.

  • a function get_passwords() which gets jQuery.attr("href") from anchor $("a.active") and makes a $.post to the server with a callback that inserts values into a table upon receiving the data

The function get_passwords() is called synchronously in the callback(event) function like this:

event.preventDefault(); //prevents a browser navigation
$("a.active").removeClass("active"); //removes active class from all other anchors
$(this).addClass("active"); //adds a class "active" to the anchor that was clicked
get_passwords(); <---- the function in question <--BREAKPOINT #1

The function get_passwords() contains the following relevant lines:

$.post("url", postData, function(data) { //put received data into table }); <--BREAKPOINT #2

Now, the weird bug is that the get_passwords() function is called multiple times by the same event listener, and only stops calling that function after an arbitrary, but constant amount of times, every time the click is made on an anchor with the event listener.

I've set the breakpoint in FireBug on the above indicated line, and as I execute the script step by step, all it does is jump between the two breakpoints set above (the part where it calls get_passwords() and where it executes $.post, 4 times (always the same amount), up until the 4th time when it actually proceeds with the callback in the $.post function.

Realizing that there is no way I can ask anyone to read through all the code that I've written, does anyone have any suggestions as to where this bug could be coming from?

Thank you in advance.

EDIT: http://jsfiddle.net/nt3A4/

7
  • Um, sounds like you are binding it multiple times/ Jul 31, 2014 at 1:16
  • That's what I thought at first, however, I did set additional breakpoints in the $.post callback function, and they are only reached upon the 4th call of the get_passwords() function. It literally just jumps between the two breakpoints above.
    – Aroll605
    Jul 31, 2014 at 1:19
  • Even if you're not posting all of your code (which you probably shouldn't), try cutting it down to a small, relevant subsection while still being able to reproduce the problem. jsfiddle.net is useful for making demos like that. As is we haven't seem your DOM or the logic for your event handler, both of which appear to be key parts of this problem.
    – nbrooks
    Jul 31, 2014 at 1:19
  • 1
    a function "events()" that has a jQuery.on('click', callback(event)) event binder inside it. That function gets called every time I add or remove anchors with AJAX <--- sounds like you're reattaching an additional click handler to all of your links each time a new one is added. Instead, consider using on for delegated events.
    – nbrooks
    Jul 31, 2014 at 1:24
  • Show binding the click event... Jul 31, 2014 at 1:28

2 Answers 2

2

Are you looking for event.stopImmediatePropagation()?

2
  • That works just as well as $("selector").off() and requires the same amount of lines of code as it has to be written in every event handler
    – Aroll605
    Aug 6, 2014 at 16:35
  • I spent my whole night on that! You are a SuperHero! Dec 16, 2014 at 7:54
0

@epascarello, @nbrooks,

You were both right. I was attaching multiple event listeners, somehow, to the same anchor. Probably got lost due to my poor coding planning (I was making up website features as I went along).

Anyway, I fixed it by pre-pending $("a").off() in the events() function, as to remove any other event listeners that may have been set before. Should of done that from the beginning :/

Thank you all for your answers and your time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.