当找到的数据输出发生时,我将脚本与事件处理程序连接。但是在我再次查找数据之后,脚本再次连接,处理程序再次挂起。如何避免这种情况?
$(document).on("click", "a[data-action='create']", function (e) {
e.preventDefault();
newContactInput(this);
});
答案 0 :(得分:1)
首先请确保newContactInput
返回一个Promise
,并在解析完成后对其进行解析。然后,在单击时,可以将被单击的元素放入Set
中,并在调用Set
之前检查元素是否不在newContactInput
中。 newContactInput
解析后,将其从集合中删除。例如:
const ongoingActions = new Set();
$(document).on("click", "a[data-action='create']", function (e) {
e.preventDefault();
// An action is still in progress for this element, return immediately:
if (ongoingActions.has(this)) return;
ongoingActions.add(this);
newContactInput(this)
.then(() => ongoingActions.delete(this));
});