I am encountering some strange behavior for the following code.
function linkFunc(scope, element, attribute) {
var page = angular.element($window);
page.bind('scroll', function() {
var windowHeight = "innerHeight" in window ? window.innerHeight : document.documentElement.offsetHeight;
var body = document.body, html = document.documentElement;
var docHeight = Math.max(body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight);
var windowBottom = windowHeight + window.pageYOffset;
if (windowBottom >= docHeight) {
scope.$apply(attribute.myDirective);
}
});
}
The above is a piece of code that detects if the bottom of the page is reached, if its reached it will call whatever function bind to myDirective
The main issue is that most of the time the lazy loading works, and myDirective
gets sucessfully called. However some of the times the lazy loading won't work, and I wasn't able to reproduce the bug.
I tried different screen size, different browser, but it seems like the bug just happens randomly.
Maybe someone have this happened to them before, and can point me a direction?
Edit:
More information
I was able to reproduce the bug after a bit of experimenting.
Basically, when the zoom in percentage of the browser is < 100 %
, window.pageY
returns a decimal value that is slightly inaccurate which cause windowBottom
to be off by a 0.1
to 0.9
eg.
console.log(windowBottom); // 1646.7747712336175
console.log(docHeight); // 1647
Does anyone know why this happens?
Edit 2:
The above behavior is also non deterministic, but the decimal part is true.
If you're not sure if lazy loading is working correctly, you can open the Chrome DevTools and check that the images are not loaded until the scroll.
Why do we need Lazy Loading in Angular 4? Lazy loading is a technique in Angular that allows you to load JavaScript components asynchronously when a specific route is activated. It improves the speed of the application load time by splitting the application into several bundles.
To lazy load Angular modules, use loadChildren (instead of component ) in your AppRoutingModule routes configuration as follows. content_copy const routes: Routes = [ { path: 'items', loadChildren: () => import('./items/items. module'). then(m => m.
If you want to check how lazy loading works and how lazy loading routing flow, then Augury is the best tool we have. Click on ctrl+F12 to enable the debugger and click on the Augury tab. Click on the router tree. Here, it will show the route flow of our modules.
0.1 + 0.2 !== 0.3
This one is an oddity not just in JavaScript; it’s actually a prevailing problem in computer science, and it affects many languages. The output of this is 0.30000000000000004.
This has to do with an issue called machine precision. When JavaScript tries to execute the line above, it converts the values to their binary equivalents. This is where the problem starts. 0.1 is not really 0.1 but rather its binary equivalent, which is a near-ish (but not identical) value. In essence, as soon as you write the values, they are doomed to lose their precision. You might have just wanted two simple decimals, but what you get, as Chris Pine notes, is binary floating-point arithmetic. Sort of like wanting your text translated into Russian but getting Belorussian. Similar, but not the same.
You can read more here. Without digging into browser source, I would guess that your problem stems from this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With