+3  A: 

Edit based on updated question:

The issue is that you're calling $.ajaxComplete() in your callback. You should get rid of that. The anonymous callback function is all you need.

The purpose of $.ajaxComplete() is to setup a default function to run when any ajax request completes. If this is what you want, you should take it out of the current callback, and just place it in the .ready() call so that it only runs once.

The way you're doing it right now, for each click (and successful response) you're adding another identical handler. So after you click 5 times, it now has 5 of the same click handler.

From the docs: http://api.jquery.com/ajaxComplete/

Whenever an Ajax request completes, jQuery triggers the ajaxComplete event. Any and all handlers that have been registered with the .ajaxComplete() method are executed at this time.


Original answer

This must not be your actual code, because it won't run at all the way you have it.

You don't have a function declaration for your function. You need to initialize i with a value like 0, and your closing parenthesis for the click handler is misplaced.

That said, your code works fine for me when corrected.

Try it out: http://jsfiddle.net/sGWjL/1/

If the alert pops up 5 times for you, then you need to paste more (or actual) code being used.

var i = 0;
var k = 5;

$('document').ready(function () {
    $('#someElement').click(function (e) {
        e.stopImmediatePropagation();
        i++;
        if (i >= k) {
            my_function();
        }
    });
});

function my_function() {
    alert(i);
    $('#myDisplay').text("You have clicked on '#someElement' " + i + "times");
}​
patrick dw
Thank you for the prompt replies! I really like that jsfiddle.net app, by the way. ;)I'll also make an effort to indent my code before posting.Aside from the elementary mistake I made with the undeclared variable, the code did indeed work correctly.I've have edited my original post, to present a code example that's more reflective of the problem I've encountered.Cheers to all of you. :)
Leonard
@Leonard - I updated my answer (see above). Basically you either need to get rid of `.ajaxComplete()`, and just call `my_function()` in your callback, or if you want `.ajaxComplete()`, only call it *once*. Right now you're calling it once for *each* ajax response.
patrick dw
A: 

OP here. :)

I basically took your advice to the letter, and removed the 'ajaxComplete()' call.

My problem was in thinking that 'ajaxComplete()' was the only way to find out if the request was successful, but that's a function of the original ajax ('get()') call anyway! ^_^

Thank you for your help!

Leonard
@Leonard - You're welcome. :o) Just a couple things. Please remember to use the comment functionality to correspond. Probably better if you delete this answer. If you leave a comment for a user under their answer, they will receive a notification and be able to respond. The second thing is to remember to click the checkbox next to an answer in order to confirm that it resolved (or helped resolve) your solution. :o)
patrick dw