Function statement being called 1+ times after AJAX request?
<p>This is an odd title, but I'm not sure how to describe what's happening. </p>

<p>Basically, I have the following code which runs after an AJAX request is made:</p>

<pre><code>if (doAjax) {
    $("#loading").removeClass("visible");
    $("#loading").addClass("hidden");
    setTimeout(function () {
        $("#loading").css({
            "display": "none",
            "opacity": "0",
        });
    }, 300);
    if (number.length === 0) {
        $("#nothing").css("display", "inline-table");
        setTimeout(function () {
            $("#nothing").addClass("visible");
        }, 1);
    } else {
        setFavorite();
        checkFavorite();
    }
}
</code></pre>

<p>If the AJAX request returns results, then it will run the <code>setFavorite();</code> and <code>checkFavorite();</code> functions once it completed. I also have the ability to reload the AJAX request when clicking a button, which would then also run those functions at the end of the request.</p>

<p>Problem is, if the user loads the page, it will run the AJAX, and will be fine. If the user <strong>calls</strong> the AJAX request again, however, the function is ran twice. If it is called once more, it is ran three times. </p>

<p><strong>So</strong>, each time the AJAX request is made, the number of times <code>setFavorite();</code> and <code>checkFavorite();</code> are called increases by one.</p>

<p>I confirmed this by placing an <code>alert</code> within <code>setFavorite()</code> to see why it was calling so randomly, and it would always be called x number of times where x is the number of times the user has reloaded the AJAX request.</p>

<p>If needed, I will post the <code>setFavorite();</code> and <code>checkFavorite();</code> functions, but I feel like the error lies within the <code>doAjax</code> function.</p>

<p><strong>Edit 1</strong></p>

<p>Reload function:</p>

<pre><code>function reload(){
    doAjax("http://www.codekraken.com/testing/snowday/wgrz.html");
};

$("#reload").click(reload);
</code></pre>

<p><strong>Edit duo</strong></p>

<p><code>doAjax();</code></p>

<pre><code>function doAjax(url) {
    $.getJSON("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D%22" + encodeURIComponent(url) + "%22&amp;format=xml'&amp;callback=?",
    function (data) {
        if (data.results[0]) {
            $("#content").html("");
            var number = $(filterData(data.results[0])).find("#gtv_leftcolumn table:gt(1)");
            for (var i = 0; i &lt; number.length; i++) {
                var school = $(filterData(data.results[0])).find("#gtv_leftcolumn table:gt(1) .maintext p:eq(" + i + ")").text();
                var type = $(filterData(data.results[0])).find("#gtv_leftcolumn table:gt(1) .trafficbriefs:nth-child(even) p:eq(" + i + ")").text();
                $("#content").append("&lt;div class='row'&gt;&lt;div class='row-inside'&gt;&lt;div class='row-l'&gt;" + school + "&lt;/div&gt;&lt;div class='row-r'&gt;" + type + "&lt;/div&gt;&lt;/div&gt;&lt;div class='star'&gt;&lt;div class='star-inside'&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;");
            };
            if (doAjax) {
                $("#loading").removeClass("visible");
                $("#loading").addClass("hidden");
                setTimeout(function () {
                    $("#loading").css({
                        "display": "none",
                        "opacity": "0",
                    });
                }, 300);
                if (number.length === 0) {
                    $("#nothing").css("display", "inline-table");
                    setTimeout(function () {
                        $("#nothing").addClass("visible");
                    }, 1);
                } else {
                    setFavorite();
                    checkFavorite();
                }
            }
        } else {
            console.log("error");
        }
    })
}

function filterData(data) {
    data = data.replace(/&lt;?\/body[^&gt;]*&gt;/g, '');
    data = data.replace(/[\r|\n]+/g, '');
    data = data.replace(/&lt;--[\S\s]*?--&gt;/g, '');
    data = data.replace(/&lt;noscript[^&gt;]*&gt;[\S\s]*?&lt;\/noscript&gt;/g, '');
    data = data.replace(/&lt;script[^&gt;]*&gt;[\S\s]*?&lt;\/script&gt;/g, '');
    data = data.replace(/&lt;script.*\/&gt;/, '');
    data = data.replace(/&lt;img[^&gt;]*&gt;/g, '');
    return data;
}
</code></pre>
