Ensure a function is only executed one time on an element
This code shows how to avoid two executions of a function on an single element. Moreover, the CSS class allows us to confirm the launch of this function.
function set_element(el) {
var testClass = 'has-func-set-element';
if (el.hasClass(testClass)) {
return false;
}
el.addClass(testClass);
// el.children('.child').each(function())
return true;
}
set_element($('test'));
// Returns true
set_element($('test'));
// Returns false
This code can be used in MooTools or jQuery.
Written by Kévin / Darklg
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Js
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#