我需要根据点击内容过滤一些项目。为此,我需要将异步回调设置为单击的attr。因为目前,仅我的全局变量不起作用。有人可以告诉我为什么这种“等待/等待”表示法不起作用吗?预先谢谢!
/* Global Variable */
var globalVariable;
/* Loading Images */
function GenerateItems(max){
var items = [];
var img_src =[];
while (img_src=Imgs1.shift()) {
//Once the global variable is promised then this action can be fulfilled
await if (globalVariable != "ImgsName") continue;
items.push('<div class="grid-item c'+2+'"><img src="'+img_src+'"/></div>');
if (items.length === max) return $(items.join(""));
}
/*SimpleInfiniteScroll */
function Infinite(e){
if((e.type == 'scroll') || e.type == 'click'){
var doc = document.documentElement;
var top = (window.pageYOffset || doc.scrollTop) - (doc.clientTop || 0);
var bottom = top + $(window).height();
var docBottom = $(document).height();
if(bottom + 10 >= docBottom){
let items = GenerateItems(8);
if(items.length == 0){
// out of items
$(window).off('scroll', Infinite);
} else {
$grid.revealItems(items);
}
}
}
}
$grid.revealItems(GenerateItems(8));
/* Click Function */
$(document).on('click','.filter-item', async function clickFilter(){
$('.filter-item.active').removeClass('active')
$(this).addClass('active');
var f = $(this).attr('data-f');
const globalVariable = new Promise(function(resolve, reject) { $(this).attr('name')};
$grid.isotope({filter: f})
});
return globalVariable;
};
使用异步/等待功能的任何功能都需要在函数定义之前包括“ async”,在函数调用之前包括“ await”。
async function GenerateItems(max){
此外,我不确定这是用来做什么的,您可能需要进一步详细说明。在下面的代码中,“ await”应位于响应回调或promise的API调用或某种函数调用的前面。这似乎也没有。
await if (globalVariable != "ImgsName") continue;
这是异步等待代码块的示例:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function
希望这会有所帮助,也许可以在等候行中详细说明您要做什么,以便我们进一步为您提供支持。