英文:
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_proceed
在 func_check_data
成功通过 ajax 保存数据之前被触发。
所以,有没有任何函数可以用来延迟后续事件,直到第一个事件完成?
英文:
I'm using css classes for firing events/functions.
<button class="func_check_data func_proceed">
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 ofif/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 ofif/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')
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论