我正在使用7个峰值的nop Commerce ajax过滤器插件。 我想将ajax请求延迟2秒,但似乎没有任何效果。 -我尝试了jquery延迟。 -我设法获得了返回诺言正常工作的睡眠功能,但在IE中不起作用。 -我已经尝试过setTimeout,但是由于某种原因,它破坏了插件。
以下是我正在从Filters.min.js文件中查看的代码:
function c(i, a, o, s, c, p, u) {
if (!r.waitForAjaxRequest && "#/noFilterApplied" !== s) {
this.isProductRefreshTriggeredByScroll = u || !1, void 0 == s ? s = "" : p = !0, this.setSpecificationFilterSelection(i, a);
var f = this.getSpecificationFiltersModel(),
h = this.getAttributeFiltersModel(),
g = this.getManufacturerFiltersModel(),
v = this.getVendorFiltersModel(),
S = this.getOnSaleFiltersModel(),
F = this.getPriceRangeFilterModel();
void 0 == o && (o = null);
var P = e(".sortOptionsDropDown").val() || e(".nopAjaxFilters7Spikes").attr("data-defaultsortoption") || null,
A = d(),
x = e(".productsPageSizeDropDown").val() || null,
k = t.getUrlVar("pageNumber");
void 0 == k || null != o || void 0 != c && c !== !1 || (o = k), void 0 == p && (p = !1);
var m = {
categoryId: this.categoryId,
manufacturerId: this.manufacturerId,
vendorId: this.vendorId,
priceRangeFilterModel7Spikes: F,
specificationFiltersModel7Spikes: f,
attributeFiltersModel7Spikes: h,
manufacturerFiltersModel7Spikes: g,
vendorFiltersModel7Spikes: v,
pageNumber: o,
orderby: P,
viewmode: A,
pagesize: x,
queryString: s,
shouldNotStartFromFirstPage: p,
onSaleFilterModel: S,
keyword: this.keyword,
searchCategoryId: this.searchCategoryId,
searchManufacturerId: this.searchManufacturerId,
priceFrom: this.priceFrom,
priceTo: this.priceTo,
includeSubcategories: this.includeSubcategories,
searchInProductDescriptions: this.searchInProductDescriptions,
advancedSearch: this.advancedSearch,
isOnSearchPage: this.isOnSearchPage
};
this.isProductRefreshTriggeredByScroll || this.showProductPanelAjaxBusy();
var C = this,
y = this.getFilteredProductsUrl;
r.waitForAjaxRequest = !0, e.ajax({
cache: !1,
type: "POST",
data: e.toJSON(m),
contentType: "application/json; charset=utf-8",
url: y
}).done(function(t) {
"" !== s && (C.changeViewModeOrderByAndPageSize(t), C.setPriceRangeValues(t));
var i = C.refreshProducts(t, C.isProductRefreshTriggeredByScroll);
if (!i) return void(r.waitForAjaxRequest = !1);
if (C.refreshFilters(t), C.refreshProductsPager(t), C.refreshClearButtons(), "NO HASH QUERY" !== s) {
C.expandFilterPanelsWithSelectedOptions();
var a = e(t).filter("#urlHashQuery").val().toString();
"" !== a ? (C.isProductRefreshTriggeredByScroll && (a = a.slice(0, a.indexOf("&pageNumber"))), e.address.value(a)) : e.address.value("noFilterApplied")
}
C.hideProductPanelAjaxBusy(), r.waitForAjaxRequest = !1;
var o = e(".nopAjaxFilters7Spikes").attr("data-scrollToElement");
if (!C.isProductRefreshTriggeredByScroll && void 0 != o && "true" === o.toLowerCase()) {
var d = e(".nopAjaxFilters7Spikes").attr("data-scrollToElementSelector"),
c = d.split(","),
p = ".product-selectors";
if (void 0 != c && c.length > 0) {
for (var u = 0; u < c.length; u++)
if (e(c[u].trim()).length > 0) {
p = c[u].trim();
break
}
e(p).length > 0 && e("html, body").animate({
scrollTop: e(p).offset().top
}, "slow")
}
}
e(".productPanelAjaxBusy").length < 1 && C.addProductPanelAjaxBusyToPage();
var f = e(t).filter("#totalCount").val().toString();
if (e.event.trigger({
type: "nopAjaxFiltersFiltrationCompleteEvent",
totalCount: f
}), "true" === e(".nopAjaxFilters7Spikes").attr("data-showSelectedFiltersPanel").toLowerCase() && l(), "true" !== e(".nopAjaxFilters7Spikes").attr("data-enableinfinitescroll").toLowerCase() && "false" !== e(".nopAjaxFilters7Spikes").attr("data-numberofreturnedproductsselector").toLowerCase()) {
var h = x;
e(".page-body .product-grid:not(.featured-product-grid)").length > 0 ? h = e(".page-body .product-grid:not(.featured-product-grid) .item-box").length : e(".page-body .product-list").length > 0 && (h = e(".page-body .product-list .item-box").length), n(h, f)
}
}).fail(function() {
C.hideProductPanelAjaxBusy(), alert("Loading the page failed."), r.waitForAjaxRequest = !1
}).always(function() {
C.isProductRefreshTriggeredByScroll = !1
})
}
}
我尝试将if语句包装在setTimeout内部的函数中,如下所示:
function c(i, a, o, s, c, p, u) {
setTimeout(function () {
if (!r.waitForAjaxRequest && "#/noFilterApplied" !== s) {
...
也尝试过:
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function c(i, a, o, s, c, p, u) {
await sleep(2000);
....
不是很确定如何去做,有什么想法吗? 干杯
1 控制台中根本没有错误,基本上是因为停止工作,我的意思是不会触发在过滤器更改时触发的ajax调用,并且不会发生任何事情。错误是我寻找的第一件事,这就是为什么我很沮丧。
2 使用睡眠承诺代码,过滤器可以正常工作,因此添加延迟不会引起问题。
3 我要修改的原因是说我有10个复选框,我想选择3个。因此,我单击了3个复选框,但很快我单击发送ajax调用的第一个复选框,而不使用其他两个复选框过滤器。这是插件发布的方式。
演示 ,使想法进入this demo,打开开发工具并限制与GPRS的连接速度,然后尝试选择2个复选框,您将看到你不能。我希望延迟,以便用户可以在发送ajax调用之前选择2或3个复选框。感谢您的帮助:)