Created
July 14, 2017 18:22
-
-
Save rosston/3b4684ca7367fc84f264f61d252b6ec2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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