javascript - Is it bad to execute a jQuery script on an event that doesn't always happen? -
i have page may or may not have alerts like:
<div class="alert">some alert</div>
and hiding them after 5 seconds with:
<script type="text/javascript"> $(document).ready(function () { window.settimeout(function() { $(".alert").fadeto(1500, 0).slideup(1500, function(){ $(this).remove(); }); }, 5000); }); </script>
so i'm wondering, should put alert hiding function general functions executed when page loads , if there no alert hide? or put in html code right below actual alert div code happens when alert shows?
the upside of first option code more organized. downside may execute if there no alert.
the upside of second option executes when alert shows, downside code messier.
which better?
there's nothing bad more efficient removing settimeout
:
$(".alert").delay(5000).fadeto(1500, 0).slideup(1500, function(){ $(this).remove(); });
this way, nothing done, , no callback registered if there's no alert.
as location of code, should avoid having in middle of html. interlacing html , javascript bad practice hindering maintainability. clean modular javascript should solution instead.
Comments
Post a Comment