如何在使用jQuery的stopImmediatePropagation()后继续触发事件?

huangapple go评论63阅读模式
英文:

How can I proceed firing events after using stopImmediatePropagation() in jQuery?

问题

我正在使用CSS类来触发事件/函数。

<button class="func_check_data func_proceed">

按钮在单击事件上调用不同的函数。

func_check_data() 检查有效数据并在成功时保存它。

因此,当函数 func_check_data() 抛出错误时,不应触发 func_proceed()

通过在 func_check_data 中使用 event.stopImmediatePropagation(),我可以使这个工作。

但是,当 func_check_data 不抛出错误时,func_proceedfunc_check_data 成功通过 ajax 保存数据之前被触发。

所以,有没有任何函数可以用来延迟后续事件,直到第一个事件完成?

英文:

I'm using css classes for firing events/functions.

&lt;button class=&quot;func_check_data func_proceed&quot;&gt;

The button calls different functions on the click event.

func_check_data() checks valid data and saves it on success.

So when function func_check_data() throws an error, func_proceed() shouldn't be fired.

By using event.stopImmediatePropagation() in func_check_data I get this working.

But, when func_check_data doesn't throw an error, func_proceed is fired, before func_check_data has saved the data successfully via ajax.

So is there any function which can be used to delay following events until the first one is finished?

答案1

得分: 0

不。您需要一个单一的事件处理程序来完成这个协调,而不是多个不协调的事件处理程序。

例如:

  • .func_check_data,而不是.func_proceed元素,可以调用检查数据函数处理程序。
  • 类似地,.func_proceed,而不是.func_check_data元素,可以调用继续函数处理程序。
  • .func_check_data.func_proceed元素(例如,同时具有这两者的元素)将使用第三个处理程序来执行数据检查和ajax保存,然后当ajax保存完成时,执行继续部分。
英文:

No. You'll want to have a single event handler that does this coordination, rather than multiple uncoordinated event handlers.

For instance:

  • .func_check_data that aren't also .func_proceed elements can call the check data function handler.
  • Similarly, .func_proceed that aren't also .func_check_data elements can call the proceed function handler.
  • .func_check_data.func_proceed elements (e.g., ones that are both) would use a third handler that does the data check and ajax save, then when the ajax save is complete, does the proceed part.

答案2

得分: 0

The typical way to handle this kind of chaining logic is either:

  • a switch or a block of if/else's that routes the flow from one function to the other by manually calling the other function, or
  • an ajax request/response, where the response runs the follow-up function

e.g.

function follow_up(data) { /* do stuff */ }
function cancel(data) { /* undo stuff */ }
function clickHandler(e) {
  let url = 'foo';
  let data = {bar: 'baz'};
  $.ajax({
    url: url, data: data, method: 'POST',
    success: follow_up,  // see funcs above
    error: cancel,       // see funcs above
  });
}

Also, be aware you can manually trigger specific events with $.trigger('eventName').

英文:

The typical way to handle this kind of chaining logic is either:

  • a switch or a block of if/else's that routes the flow from one function to the other by manually calling the other function, or
  • an ajax request/response, where the response runs the follow-up function

e.g.

function follow_up(data) { /* do stuff */ }
function cancel(data) { /* undo stuff */ }
function clickHandler(e) {
  let url = &#39;foo&#39;;
  let data = {bar: &#39;baz&#39;};
  $.ajax({
    url: url, data: data, method: &#39;POST&#39;,
    success: follow_up,  // see funcs above
    error: cancel,       // see funcs above
  });
}

Also, be aware you can manually trigger specific events with $.trigger(&#39;eventName&#39;).

huangapple
  • 本文由 发表于 2020年1月6日 23:54:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/59615136.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定