javascript - jQuery: avoid code duplication because of fadeOut() -
here's problem i've faced many times:
- i have element may or may not visible
- i need stuff element
- if element visible,
fadeout()
element - once stuff done:
fadein()
element
the problem here: code looks this:
function showonlyelement(myel) { $('body') .children('div:not(.'+myel+')') .fadeout() .promise().done(function() { var el=b.children('div.'+myel); if (el.is(':visible')) { /* have hide before modifying */ el.fadeout(function() { /* long code (a) modifying innerhtml of el */ el.fadein(); }); } else { /* again same long code (a) modifying innerhtml of el */ el.fadein(); } }); }
i want make clean there's not repetition of same long code (a)
how do (generic way of doing this)?
simple generic solution :
$('body') .children('div:not(.'+myel+')') .fadeout() .promise().done(function() { var el=b.children('div.'+myel); function longcode(){ /* long code (a) modifying innerhtml of el */ el.fadein(); } if (el.is(':visible')) { el.fadeout(longcode); } else { longcode(); } });
Comments
Post a Comment