Search code examples
javascriptjquerystack-overflow

Getting the "RangeError: Maximum call stack size exceeded" error


I am getting this JavaScript error in Chrome:

RangeError: Maximum call stack size exceeded [http://localhost:5545/assets/js/jquery.min.js:2]

With this click event:

$('.accept-answer-button').click(function() {
    var id = $(this).parent().parent().attr('id').replace('answer_', ''),
        b = this;
    if ($('.accepted').length > 0) $('.accepted').click();
    AP.AcceptAnswer(id, $(b));
});​

The code for AP.AcceptAnswer() is:

AP.AcceptAnswer = function(a, o) {
    $.getJSON('/assets/ajax/accept-answer', {
        qid: qid,
        answer: a
    }, function(data) {
        if (data.success == true) {
            if (data.type == 'accepted') {
                var title = o.attr('data-unaccept-title').replace('{t}', data.time);
                o.addClass('accepted').attr('title', title);
            }
            else if (data.type == 'unaccepted') {
                var title = o.attr('data-accept-title').replace('{t}', data.time);
                o.removeClass('accepted').attr('title', title);
            }
        }
        else {
            alert(data.error);
        }
    });
}; // end AP.AcceptAnswer()​

I am doing $('.accepted').click(); just to unaccept the currently accepted answer (if there is one) when the user would like to accept another answer.


Solution

  • Are you sure that the classes .accepted and .accept-answer-button aren't pointing to the same element? If they are you are recursively firing a click event when you do this:

    $('.accepted').click(); 
    

    If $('.accepted').length > 0 is always true.