Tuesday, April 15, 2014

Recursively scroll

'use strict';
commonLib.factory('scrollHelper', ['$timeout', function ($timeout) {
//scroll to element recursively until found
function scrollToElement(scrollToId, maxRetry) {
var counter = 0;
function tryScroll() {
counter ++;
var el = document.getElementById(scrollToId);
if (el) {
el.scrollIntoView(true);
} else if (counter < maxRetry) {
//try scroll again
$timeout(tryScroll, counter * 10);
}
};
$timeout(tryScroll, 0);
};
return {
scrollToElement: scrollToElement
};
}]);