JavaScript Allow various functions to execute only if some condition is true
I have a list of event handlers that are attached to various elements, but
I want to disable some of them when a certain condition is true. This
condition (i.e. boolean value) changes dynamically and it is not
predictable when it changes. Here is what I do currently.
function foo () {
if (someCondition) {
return;
}
// foo does something
}
function bar () {
if (someCondition) {
return;
}
// bar does something
}
...etc
This is alright, but it's really redundant to have the if block in each
function. Is there a more concise way to manage this? I was wondering if I
can attach two event handlers to one element, and only execute one if the
other returned true.
No comments:
Post a Comment