是否可以用getter / setter替换此超时。我需要过滤器库中的图像进行相应的排序,方法是等待用户单击某个部分时找出globalVariable是什么。
现在,我使用一个超时,单击一次后不起作用。 Here is one question我看着,但是我仍然很难绕着它。 Here is the codepen。什么是实现这一目标的最佳方法?
部分代码:/ *在“全部”上生成所有项目,然后单击以生成与globalVariable相匹配的项目* /
$grid.revealItems(GenerateItems(8));
/*This click function doesn't repeat, which means no more items load after second click*/
var variableSet = document.getElementById("filterList");
variableSet.addEventListener('click', runFunction);
function runFunction(){setTimeout (function(){
$grid.revealItems(GenerateItems(4));
}, 1000)}
/* Filter on Click */
$(document).on("click", ".filter-item", function clickFilter() {
$(".filter-item.active").removeClass("active");
$(this).addClass("active");
var f = $(this).data("f");
//Global Variable
globalVariable = $(this).attr('name');
使用异步/等待功能的任何功能都需要在函数定义之前包括“ 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
希望这会有所帮助,也许可以在等候行中详细说明您想做的事情,以便我们进一步为您提供支持。