One solution would be to set up a locking mechanism to stop fast requests to your server. The lock will be enabled before the request is executed, and then the lock will be disabled when the request is completed and the DOM will be updated with new content (which extends the size of your page).
For example:
new Vue({ // ... your Vue options. ready: function () { var vm = this; var lock = true; window.addEventListener('scroll', function () { if (endOfPage() && lock) { vm.$http.get('/myBackendUrl').then(function(response) { vm.myItems.push(response.data); lock = false; }); }; });
}; });
Another thing to keep in mind is that the scroll event fires more than you really need (especially on mobile devices), and you can throttle this event to increase performance. This can be done efficiently with requestAnimationFrame:
;(function() { var throttle = function(type, name, obj) { obj = obj || window; var running = false; var func = function() { if (running) { return; } running = true; requestAnimationFrame(function() { obj.dispatchEvent(new CustomEvent(name)); running = false; }); }; obj.addEventListener(type, func); }; throttle ("scroll", "optimizedScroll"); })();
ashish bansal
source share