jquery - How to ReRun a Function? -
how make function recallable? right now, reruns after page reloaded. want work time. apparently, not using unbind right.
<script> $(document).ready(function() { $('.viewall').bind("click.myevent", function(){ $('.mobilenavbtn, #shopnavtabbtn, #sec0').click(); $(this).unbind("click.myevent"); $('html, body').animate({ scrolltop: $("#sec0").offset().top }, 2000); return false; }); }) </script> this original function:
<script> $(document).ready(function() { $('.viewall').on('click', function(){ $('.mobilenavbtn, #shopnavtabbtn, #sec0').click(); $('html, body').animate({ scrolltop: $("#sec0").offset().top }, 2000); return false; }); }) </script>
$(this).unbind("click.myevent"); here unbinding event listener. result of event firing once (as stop listening after first time triggered.)
a nicer cleaner way of achieving functionality in jquery attach event handler using jquery 1 method.
on other hand, if trying listen element while animation not running, attach event listener, , remove when animation begins. reattach event after animation. example provided this:
$('.viewall').on('click', viewallclick); function viewallclick(evt) { $('.mobilenavbtn, #shopnavtabbtn, #sec0').click(); //detach event $('.viewall').off('click', viewallclick); $('body').animate({ scrolltop: $("#sec0").offset().top, }, { done: function () { //reattach event $('.viewall').on('click', viewallclick); }, duration: 2000 }); } i double check animate call ensure doing intend.
see plunker similar example. click on text , observed animate logged after animation event reattached, while clicked fire every time click text.
Comments
Post a Comment