Skip to content

Instantly share code, notes, and snippets.

@AlexJWayne
Created November 17, 2011 21:43
Show Gist options
  • Star 28 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save AlexJWayne/1d99b3cd81d610ac7351 to your computer and use it in GitHub Desktop.
Save AlexJWayne/1d99b3cd81d610ac7351 to your computer and use it in GitHub Desktop.
# Accurate Interval, guaranteed not to drift!
# (Though each call can still be a few milliseconds late)
window.accurateInterval = (time, fn) ->
# This value is the next time the the timer should fire.
nextAt = new Date().getTime() + time
# Hold a reference to the timer ID so it can be canceled.
timeout = null
# Allow arguments to be passed in in either order.
if typeof time is 'function'
[fn, time] = [time, fn]
# Create a function that wraps our function to run. This is responsible for
# scheduling the next call and aborting when canceled.
wrapper = ->
nextAt += time
timeout = setTimeout wrapper, nextAt - new Date().getTime()
fn()
# Clear the next call when canceled.
cancel = -> clearTimeout timeout
# Schedule the first call.
timeout = setTimeout wrapper, nextAt - new Date().getTime()
# Return an object that contains the cancel() function so we can later cancel.
return { cancel }
(function() {
window.accurateInterval = function(time, fn) {
var cancel, nextAt, timeout, wrapper, _ref;
nextAt = new Date().getTime() + time;
timeout = null;
if (typeof time === 'function') _ref = [time, fn], fn = _ref[0], time = _ref[1];
wrapper = function() {
nextAt += time;
timeout = setTimeout(wrapper, nextAt - new Date().getTime());
return fn();
};
cancel = function() {
return clearTimeout(timeout);
};
timeout = setTimeout(wrapper, nextAt - new Date().getTime());
return {
cancel: cancel
};
};
}).call(this);
// Use like setTimeout
var timer = accurateInterval(function() {
console.log('message you will see every second!');
}, 1000);
// Or provide the time first and function second (much nicer in coffee script)
var timer = accurateInterval(1000, function() {
console.log('message you will see every second!');
});
// Cancel the interval later by simply calling the cancel method.
timer.cancel();
Copyright (C) <year> by <copyright holders>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
@rbrtbrnschn
Copy link

Works like a charm, thanks man!

@beginner-cryptonyx
Copy link

typescript plz?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment