Skip to content

Instantly share code, notes, and snippets.

@rosston
Created July 14, 2017 18:22
Show Gist options
  • Save rosston/3b4684ca7367fc84f264f61d252b6ec2 to your computer and use it in GitHub Desktop.
Save rosston/3b4684ca7367fc84f264f61d252b6ec2 to your computer and use it in GitHub Desktop.
/*
* Doubly-nested timeout ensures we run after browser render. (Sometimes needs
* to be triply-nested timeout in IE/Edge, for reasons unknown to me.)
* Explanation below.
*
* 1. $watch runs, sets up $timeout to be run later (see NOTE below)
* 2. $watch finishes (i.e., Angular has finished manipulating the DOM)
* 3. First $timeout runs, sets up $timeout to be run later (see NOTE below)
* 4. Browser renders
* 5. Second $timeout runs!
*
* NOTE: JS is single-threaded, so $timeout will run after next break in
* processing
*/
$scope.$watch('watch_something',
function(newValue, oldValue){
if(newValue !== oldValue){
// Doubly-nested timeout ensures we run after browser render
$timeout(function(){
$timeout(function(){
// Do stuff here
}, 0, false);
}, 0, false);
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment