Jim Lynch Codes
  • Blog
  • About Jim

Writings about one coder's stories & experiences.

Fast DOM Loading with AngularJS and $timeout

4/11/2016

0 Comments

 
I was working on an Angular 1.4 project recently, and it was taking a crazy long time to load. It was a truly single page application (no routes) that ran as a web page inside of a corporate CMS program. Basically, a large corp would give it's employees Ipads, and all of the company apps would be "subapps" inside of the corporate container app.

How to Get an Embarrassingly Long Load Time

Sometimes you want to stretch out the development time of your project so you can get more money (just kidding! That's pretty messed up!). Really, I'm just showing you this because I made this mistake, and I don't want you to make it and wondering why your app is so slow to load. What I was doing was creating controllers and putting async calls directly in the controller's constructor, with no use of $timeout. In my situation the app was taking about 12 seconds to load! It would first be a white screen. Then it would show a weird half-finished version of how the DOM should look, and then finally my ready-to-use app would be displayed in all it's glory. For a web page, this is unacceptably long, and I now I know how prevent async calls from holding up the DOM rendering.

Those Dag Nabbit Asyncronous Calls

Asynchronous calls take time. That's the whole point of using them. You call out to something- a REST api, loading from the file system, and interval goes off... You really don't want your entire DOM rendering to be held up by an async event call, and really it never needs to be. It's easy to make rookie mistakes in Angular, though, and this is a pretty common one (luckily, there's an easy fix!). So, I just put a random async call in my controller, and now my DOM is all whacked out when it loads. What should I do?

$timeout to the Rescue!

Thanks to a post by a SO user, parliament, I discovered that if you use the $timeout service with no delay value then it just occurs immediately, but after the SOM has rendered!  If you're curious, you can find the original SO question here:  http://stackoverflow.com/questions/11125078/is-there-a-post- render-callback-for-angular-js-directive) 
Picture
So, in the end here's how I changed my code. This is the old code, the terribly bad and slow-loading version:
angular
.module('MyApp')
.controller('SubheaderAppCtrl', function($timeout) {
        
loadMasterJson();

function loadMasterJson() {

    // Code to do async calls goes here.

​And here's the speedy new hotness:
angular
.module('MyApp')
.controller('SubheaderAppCtrl', function($timeout) {

$timeout(function(){     
    loadMasterJson();
});

function loadMasterJson() {

    // Code to do async calls goes here.

It's pretty amazing, but putting your async calls in this $timeout block will allow the page to finish rendering which makes it seem much snappier and prevents weird partial-renderings. It's important to note that this doesn't speed up the actual loading of the JSON files, and since it will take some time after the DOM renders you will probably want to put a loading spinner or some other loading indicator in the empty area that will display the data in the JSON files (if you have a visible area like that, of course). 

Don't Kick Yourself!

If you fell into this trap also, don't be ashamed. It's a little tricky, and wrapping your async calls in a $timeout with no delay does seem a bit hackish. The important thing is that know you know the right way to do it with the quickest load time; so don't forget it! :) 
0 Comments

Your comment will be posted after it is approved.


Leave a Reply.

    ​Author

    Picture
    The posts on this site are written and maintained by Jim Lynch. About Jim...
    Follow @JimLynchCodes
    Follow @JimLynchCodes

    Want FREE access to
    my daily stock tips 
    ​newsletter called,
    "The Triple Gainers"?

    Sign up here!

    Categories

    All
    Actionscript 3
    Angular
    AngularJS
    Automated Testing
    AWS Lambda
    Behavior Driven Development
    Blogging
    Business Building
    C#
    C / C++
    ClojureScript / Clojure
    Coding
    Community Service
    CS Philosophy
    Css / Scss
    Dev Ops
    Firebase
    Fitness
    Flash
    Front End
    Functional Programming
    Git
    Go Lang
    Haskell
    Illustrations
    Java
    Javascript
    Lean
    Life
    Linux
    Logic Pro
    Music
    Node.js
    Planning
    Productivity
    Professionalism
    Python
    React
    Redux / Ngrx
    Refactoring
    Reusable Components
    Rust
    Security
    Serverless
    Shell Scripting
    Swift
    Test Driven Development
    Things
    TypeScript
    Useful Sites
    Useful Tools
    Video
    Website Development
    WebStorm
    Writing

    Archives

    October 2020
    September 2020
    May 2020
    April 2020
    February 2020
    January 2020
    December 2019
    October 2019
    September 2019
    August 2019
    July 2019
    June 2019
    May 2019
    April 2019
    March 2019
    February 2019
    January 2019
    December 2018
    November 2018
    October 2018
    September 2018
    August 2018
    June 2018
    May 2018
    April 2018
    March 2018
    February 2018
    January 2018
    December 2017
    November 2017
    October 2017
    September 2017
    August 2017
    July 2017
    May 2017
    April 2017
    March 2017
    February 2017
    January 2017
    December 2016
    November 2016
    October 2016
    September 2016
    August 2016
    July 2016
    June 2016
    May 2016
    April 2016
    March 2016
    February 2016
    January 2016
    December 2015
    November 2015
    October 2015

    RSS Feed

  • Blog
  • About Jim
JimLynchCodes © 2021