diff --git a/src/geoQuery.js b/src/geoQuery.js index f0765a1d..1571eaf4 100644 --- a/src/geoQuery.js +++ b/src/geoQuery.js @@ -371,6 +371,12 @@ var GeoQuery = function (firebaseRef, queryCriteria) { for (var i = 0; i < numKeys; ++i) { var key = keys[i]; + // If the query was cancelled while going through this loop, stop updating locations and stop + // firing events + if (_cancelled === true) { + break; + } + // Get the cached information for this location var locationDict = _locationsTracked[key]; @@ -473,6 +479,9 @@ var GeoQuery = function (firebaseRef, queryCriteria) { * query via on() will be cancelled. This query can no longer be used in the future. */ this.cancel = function () { + // Mark this query as cancelled + _cancelled = true; + // Cancel all callbacks in this query's callback list _callbacks = { ready: [], @@ -516,6 +525,9 @@ var GeoQuery = function (firebaseRef, queryCriteria) { key_moved: [] }; + // Variable to track when the query is cancelled + var _cancelled = false; + // Variables used to keep track of when to fire the "ready" event var _valueEventFired = false; var _outstandingGeohashReadyEvents;