Reputation: 11767
This is an odd title, but I'm not sure how to describe what's happening.
Basically, I have the following code which runs after an AJAX request is made:
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();
}
}
If the AJAX request returns results, then it will run the setFavorite();
and checkFavorite();
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.
Problem is, if the user loads the page, it will run the AJAX, and will be fine. If the user calls the AJAX request again, however, the function is ran twice. If it is called once more, it is ran three times.
So, each time the AJAX request is made, the number of times setFavorite();
and checkFavorite();
are called increases by one.
I confirmed this by placing an alert
within setFavorite()
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.
If needed, I will post the setFavorite();
and checkFavorite();
functions, but I feel like the error lies within the doAjax
function.
Edit 1
Reload function:
function reload(){
doAjax("http://www.codekraken.com/testing/snowday/wgrz.html");
};
$("#reload").click(reload);
Edit duo
doAjax();
function doAjax(url) {
$.getJSON("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D%22" + encodeURIComponent(url) + "%22&format=xml'&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 < 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("<div class='row'><div class='row-inside'><div class='row-l'>" + school + "</div><div class='row-r'>" + type + "</div></div><div class='star'><div class='star-inside'></div></div></div>");
};
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(/<?\/body[^>]*>/g, '');
data = data.replace(/[\r|\n]+/g, '');
data = data.replace(/<--[\S\s]*?-->/g, '');
data = data.replace(/<noscript[^>]*>[\S\s]*?<\/noscript>/g, '');
data = data.replace(/<script[^>]*>[\S\s]*?<\/script>/g, '');
data = data.replace(/<script.*\/>/, '');
data = data.replace(/<img[^>]*>/g, '');
return data;
}
Upvotes: 1
Views: 191
Reputation: 669
This sounds like you are binding the reload button every time the function is ran. Which will run the function multiple times.
Are you able to show the setFavorite(); and checkFavorite(); functions?
If you have to rebind the button every reload you can unbind the button before rebinding.
Upvotes: 2