所以,我的网站上有一个实时搜索框但是当我输入另一个不相关的产品时,它有时会返回列表顶部的旧结果,所以我想知道我的代码中是否有不对的内容一些额外的代码可以修复此问题如果看起来不对劲,请问我。我很乐意回复。完整代码共享:https://codeshare.io/5eKyvL
var delay = (function () {
var timer = 0;
return function (callback, ms) {
clearTimeout(timer);
timer = setTimeout(callback, ms);
};
})();
var initSearchEvents = function () {
getElement().keyup(function (e) {
var value = $(this).val();
if (e.keyCode === 13) {
e.stopPropagation();
var data = {
title : value
};
if (value.length >= getMinchars()) {
searchRedirect(data);
}
return false;
}
delay(function () {
if (value.length >= getMinchars()) {
ajaxRequest(value);
} else {
hideResults();
}
}, 300);
});
getElement().click(function () {
var value = $(this).val();
if (value.length >= getMinchars())
ajaxRequest(value);
else
hideResults();
});
getShowAllResultsElement().click(function () {
var data = {
"title" : getElement().val()
};
searchRedirect(data);
});
}
var ajaxSuccessPagination = function (data) {
if (data.success) {
if (data.total > 0) {
setTotalItems(data.total);
showResults(data.rows);
(data.suggestions != undefined && data.suggestions.length >
0) ? showSuggestions(data.suggestions) : closeSuggestions();
} else {
noResults();
}
} else {
error("Ajax call returned false");
noResults();
}
};
showResults:
var ajaxSuccess = function (data) {
if (data.success) {
if (data.total > 0) {
setTotalItems(data.total);
setPaginationValues();
showResults(data.rows);
setExitPopupEvents();
(data.suggestions != undefined && data.suggestions.length > 0) ? showSuggestions(data.suggestions) : closeSuggestions();
} else {
noResults();
}
} else {
noResults();
}
}
var ajaxSuccessPagination = function (data) {
if (data.success) {
if (data.total > 0) {
setTotalItems(data.total);
showResults(data.rows);
(data.suggestions != undefined && data.suggestions.length > 0) ? showSuggestions(data.suggestions) : closeSuggestions();
} else {
noResults();
}
} else {
error("Ajax call returned false");
noResults();
}
};
var ajaxError = function (data) {
error("Ajax call failed");
};
var showResults = function (data) {
var jResults = getResultsElement();
$.when(getResultsElements().remove()).then(function () {
for (var i = 0; i < getLimit() && i < data.length; i++) {
var jResult = $('<li>');
jResult.append('<div class="left">' + '<a href="' + data[i].url + '">' + '<img alt="' + data[i].description + '" title="' + data[i].description + '" src="' + data[i].image_src + '" />' + '</a>' + '</div>' + '<div class="right">' + '<h4>' + '<a href="' + data[i].url + '">' +
data[i].title + '</a>' + '</h4>' + '<h5>' + '<span>vanaf</span>' + ' ' + data[i].price + '</h5>' + '</div>' + '<div class="more">' + '<a href="' + data[i].url + '">More</a>' + '</div>');
jResults.append(jResult);
}
var pageCount = getTotalItems() / getLimit();
getResultsElementWrapper().show();
});