I remember asking this question for quite some time. The answer, which was in the github repository called angular-flexslider, was that it replaced the $(document).ready(function() {...}))
component level.
Angular manipulates the DOM after the page loads. Sometimes you can see the update of angular expressions after loading pages, when the processor is a little busy. The timeout function ensures that the code runs zero milliseconds after angular completes processing the document.
I never had to do this, and these days with the component lifecycle hooks, I can't imagine what you will ever need again (that is, if you really ever needed it). But in English terms I saw this reason. I can also say that I saw a lot of Angular code written by engineers that you would be very sure of (as far as they know Angular), and never saw anyone do this.
It is also a way to make sure that the code you want to run is placed at the end of the current event loop queue. This is often done for performance. I used to do something like the following for "infinite scroll" lists (now it's all RxJS, Observables, etc.). Consider this code:
var res = [] function response ( d ) { var chunk = data.splice ( 0, 1000 ); res = res.concat ( chunk.map ( ...do something ) ); if ( data.length > 0 ) { setTimeout ( () => { response ( data ) }, 0 ); } }
Since you recursively call response
via setTimeout
to process chunks, even if it has an interval of 0, you do this in batches, and each of these batches will be processed at the end of the current job queue (instead of just creating an event stack in the event queue that will be blocked) .
source share