我已經在JavaScript中實現了setInterval()
函數,它使得每2分鐘進行一次AJAX調用。 ajax調用返回我在html中追加的數據。我不知道爲什麼,但它會逐漸使網站變得非常緩慢。該網站是在節點js中開發的。我搜索了這個問題並瞭解了clearInterval()
函數。但我無法正確使用它。有人能給我一個正確清除間隔的通用示例嗎?或者這是否與節點js有關的其他問題?Javascript setInterval與Ajax調用製作網站緩慢
代碼下面:
setInterval(function() {
appendData(skipCount);}, 120000);
function appendData(_popularSkipCount) {
var cookieValue = $.cookie('type');
var _data = {
"limit": defaultLimit - 1,
"skipCount": _popularSkipCount
}
$.ajax({
type: "GET",
url: "/popular/" + cookieValue,
data: _data,
async: true,
success: function (data) {
$("#data-box").empty();
if($(data).size() > 0) {
for(var i = 0; i < $(data).size() ; i++) {
var html = "<div class='large-12 columns popular'><img class='image' src='" + $(data)[i].url + "' class='popular-img'><a href='/abc/" + $(data)[i].slug + "'><h4>" + name + "</h4></a><h5>" + city + "</h5></div>";
$(html).hide().appendTo("#data-box").fadeIn(1000);
}
} else {
appendData("0");
}
},
error: function(jqXHR, textStatus, errorthrown) {
$("#data-box").empty();
console.log(errorthrown);
}
});
};
我的猜測是你使用'同步'ajax調用過於頻繁。從而導致網站緩慢 – Praveen
提供有關的代碼有問題本身 –
請添加您的代碼 –