删除事件目标

时间:2018-07-30 07:45:30

标签: javascript

当找到的数据输出发生时,我将脚本与事件处理程序连接。但是在我再次查找数据之后,脚本再次连接,处理程序再次挂起。如何避免这种情况?

$(document).on("click", "a[data-action='create']", function (e) {
    e.preventDefault();
    newContactInput(this);
});

1 个答案:

答案 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));
});
相关问题