javascript - jQuery / PHP - Lazy loading gallery keeps trying to fetch images even when they've all loaded -
i've got image gallery showing 1 image per row inside of div. don't want next image load until reaches edge of viewport (to save on server resources). images named sequentially in same folder (img/1.jpeg, img/2.jpeg, img/3.jpeg, ...).
i'm using modified jquery plugin this, still keeps trying fetch next image after images in directory have been loaded. it's last if statement i'm having trouble here.
how stop function running once last image in directory has loaded?
<?php // count total number of images in directory $directory = "img/"; $totalimages = count(glob("" . $directory . "*.jpeg")); ?> <script type="text/javascript"> $('document').ready(function(){ scrollalert(); }); function scrollalert(){ var scrolltop=$('#scrollbox').attr('scrolltop'); var scrollheight=$('#scrollbox').attr('scrollheight'); var windowheight=$('#scrollbox').attr('clientheight'); if(scrolltop>=(scrollheight-(windowheight))) { // fetch next image var nextimgnum=$('#content img').length + 1; $('#content').append('<img src=\"book1/'+nextimgnum+'.jpeg\" /><br />'); updatestatus(); } if(nextimgnum<=<?php echo $totalimages ?>) { settimeout('scrollalert();', 0); } } </script>
any tips optimize script appreciated :)
that script highly inefficient , keeps browser quite busy. you're in tight loop checking whether user has scrolled page , avoiding browser's script timeout via use of settimeout(). not recommend. can fix issue, why not use jquery lazy load plug-in?
Comments
Post a Comment