From 52d9aa247d2497e6d79c3f94702dd86ebf4e3751 Mon Sep 17 00:00:00 2001 From: Oleksandr Danylchenko Date: Sat, 20 Feb 2021 13:01:40 +0200 Subject: [PATCH 1/8] Removed jQuery usage from example.html --- example.html | 91 +++++++++++++++++++++++++++------------------------- 1 file changed, 48 insertions(+), 43 deletions(-) diff --git a/example.html b/example.html index bb25f4f..1f7f79d 100644 --- a/example.html +++ b/example.html @@ -1,44 +1,49 @@ - - - GeolocationThrottle Example - - -

GeolocationThrottle Example

- -

- This example will print the lat/lng of the current location, at most once every 5 seconds. -

- - -

- -
- - - - - - - \ No newline at end of file + + + GeolocationThrottle Example + + +

GeolocationThrottle Example

+

+ This example will print the lat/lng of the current location, at most once every 5 seconds. + +
+
+ + +

+
+ + + + + From 0871a13ca915ea36ec167de2e1d2180e47bf548f Mon Sep 17 00:00:00 2001 From: Oleksandr Danylchenko Date: Sat, 20 Feb 2021 13:12:00 +0200 Subject: [PATCH 2/8] Reworked geolocation-throttle.js in ES6 format --- geolocation-throttle.js | 150 +++++++++++++++++++--------------------- 1 file changed, 73 insertions(+), 77 deletions(-) diff --git a/geolocation-throttle.js b/geolocation-throttle.js index 58a0236..862ef12 100644 --- a/geolocation-throttle.js +++ b/geolocation-throttle.js @@ -1,78 +1,74 @@ -(function(window) { - window.GeolocationThrottle = { - // a mapping of watchId => timeoutId that can be used to clear position watching - _timeoutIds: {}, - - /** - * Works just like navigator.geolocation.watchPosition, but adds one extra argument - * (throttleTime) to the options object, and makes sure that only one location callback - * is made per throttleTime timespan. - * - * Assumes that the browser supports navigator.geolocation, so one should still check for - * that. The single purpose of this lib is to throttle the location callbacks. - */ - watchPosition: function(callback, errorCallback, options) { - var throttleTime = (!options ? 0 : options.throttleTime || 0); - var bufferedArguments = null; - var lastCall = null; - var timeoutId = null; - var watchId = null; - - watchId = navigator.geolocation.watchPosition(function() { - // update bufferedArguments - bufferedArguments = arguments; - - if (!lastCall) { - //console.log("calling immediately initially"); - lastCall = new Date(); - callback.apply(this, arguments); - } else if (!timeoutId) { - // check if we've already passed the buffer time, in which case - // we'll call the callback immediately - if (new Date()-lastCall > throttleTime) { - //console.log("calling immediately since time has already passed"); - lastCall = new Date(); - callback.apply(this, arguments); - } else { - // if not enough time has passed since the last callback, we'll schedule - // a callback in the future - var that = this; - //console.log("call scheduled"); - timeoutId = setTimeout(function() { - //console.log("Call"); - lastCall = new Date(); - callback.apply(that, bufferedArguments); - - timeoutId = null; - window.GeolocationThrottle._timeoutIds[watchId] = null; - }, throttleTime - (new Date()-lastCall)); - - // we store the timeout id so that we can clear the timeout if clearWatch - // is called before the setTimeout fires - window.GeolocationThrottle._timeoutIds[watchId] = timeoutId; - } - } else { - // we already have a scheduled call - //console.log("skipping call"); - } - }, errorCallback, options); - return watchId; - }, - - /** - * Calls navigator.geolocation.clearWatch for the given watchId, but also - * clears any timeout that has been set up by GeolocationThrottle to make - * sure that no more callback for this watchId is called. - */ - clearWatch: function(watchId) { - navigator.geolocation.clearWatch(watchId); - - // if there's a scheduled watch position callback we'll clear it - var timeoutId = window.GeolocationThrottle._timeoutIds[watchId]; - if (timeoutId) { - clearTimeout(timeoutId); - window.GeolocationThrottle._timeoutIds[watchId] = null; - } +{ + window.GeolocationThrottle = { + // a mapping of watchId => timeoutId that can be used to clear position watching + _timeoutIds: {}, + + /** + * Works just like navigator.geolocation.watchPosition, but adds one extra argument + * (throttleTime) to the options object, and makes sure that only one location callback + * is made per throttleTime timespan. + * + * Assumes that the browser supports navigator.geolocation, so one should still check for + * that. The single purpose of this lib is to throttle the location callbacks. + */ + watchPosition: (callback, errorCallback, options) => { + const throttleTime = (!options ? 0 : options.throttleTime || 0); + let lastCall = null; + let timeoutId = null; + + const watchId = navigator.geolocation.watchPosition(function () { + if (!lastCall) { + //console.log("calling immediately initially"); + lastCall = new Date(); + callback.apply(this, arguments); + } else if (!timeoutId) { + // check if we've already passed the buffer time, in which case + // we'll call the callback immediately + if (new Date() - lastCall > throttleTime) { + //console.log("calling immediately since time has already passed"); + lastCall = new Date(); + callback.apply(this, arguments); + } else { + // if not enough time has passed since the last callback, we'll schedule + // a callback in the future + //console.log("call scheduled"); + + const timeout = throttleTime - (new Date() - lastCall); + timeoutId = setTimeout(() => { + //console.log("Call"); + lastCall = new Date(); + callback.apply(this, arguments); + + timeoutId = null; + window.GeolocationThrottle._timeoutIds[watchId] = null; + }, timeout); + + // we store the timeout id so that we can clear the timeout if clearWatch + // is called before the setTimeout fires + window.GeolocationThrottle._timeoutIds[watchId] = timeoutId; + } + } else { + // we already have a scheduled call + //console.log("skipping call"); } - }; -})(window); + }, errorCallback, options); + return watchId; + }, + + /** + * Calls navigator.geolocation.clearWatch for the given watchId, but also + * clears any timeout that has been set up by GeolocationThrottle to make + * sure that no more callback for this watchId is called. + */ + clearWatch: watchId => { + navigator.geolocation.clearWatch(watchId); + + // if there's a scheduled watch position callback we'll clear it + const timeoutId = window.GeolocationThrottle._timeoutIds[watchId]; + if (timeoutId) { + clearTimeout(timeoutId); + window.GeolocationThrottle._timeoutIds[watchId] = null; + } + } + }; +} From 1942b6bf898fd0b02e101326afa85ab5e702560b Mon Sep 17 00:00:00 2001 From: Oleksandr Danylchenko Date: Sat, 20 Feb 2021 13:14:40 +0200 Subject: [PATCH 3/8] Moved ES6 formatted code to separate file --- example.html | 2 +- geolocation-throttle-es5.js | 78 +++++++++++++++++++++++++++++++++++++ geolocation-throttle-es6.js | 74 +++++++++++++++++++++++++++++++++++ geolocation-throttle.js | 74 ----------------------------------- 4 files changed, 153 insertions(+), 75 deletions(-) create mode 100644 geolocation-throttle-es5.js create mode 100644 geolocation-throttle-es6.js delete mode 100644 geolocation-throttle.js diff --git a/example.html b/example.html index 1f7f79d..b544678 100644 --- a/example.html +++ b/example.html @@ -16,7 +16,7 @@

GeolocationThrottle Example

    - +