From dad26037521ff681f9a3c3d4a9bebf14fb8e38cc Mon Sep 17 00:00:00 2001 From: Vojta Jina Date: Wed, 1 Jun 2011 17:38:25 +0200 Subject: Refactor $browser's lazy start polling + unit tests --- src/Browser.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'src/Browser.js') diff --git a/src/Browser.js b/src/Browser.js index 80cabe02..052ea59f 100644 --- a/src/Browser.js +++ b/src/Browser.js @@ -141,7 +141,8 @@ function Browser(window, document, body, XHR, $log) { ////////////////////////////////////////////////////////////// // Poll Watcher API ////////////////////////////////////////////////////////////// - var pollFns = []; + var pollFns = [], + pollTimeout; /** * @workInProgress @@ -162,11 +163,13 @@ function Browser(window, document, body, XHR, $log) { * @param {function()} fn Poll function to add * * @description - * Adds a function to the list of functions that poller periodically executes + * Adds a function to the list of functions that poller periodically executes, + * and starts polling if not started yet. * * @returns {function()} the added function */ self.addPollFn = function(fn) { + if (!pollTimeout) self.startPoller(100, setTimeout); pollFns.push(fn); return fn; }; @@ -187,7 +190,7 @@ function Browser(window, document, body, XHR, $log) { self.startPoller = function(interval, setTimeout) { (function check(){ self.poll(); - setTimeout(check, interval); + pollTimeout = setTimeout(check, interval); })(); }; -- cgit v1.2.3