Skip to content

Instantly share code, notes, and snippets.

@AlexJWayne
Created December 4, 2011 20:30
Show Gist options
  • Select an option

  • Save AlexJWayne/1431195 to your computer and use it in GitHub Desktop.

Select an option

Save AlexJWayne/1431195 to your computer and use it in GitHub Desktop.
accurateInterval() is a dop in replacement for setInterval() that does not drift over long periods of time.
# 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.
setTimeout wrapper, nextAt - new Date().getTime()
# Return the wrapper function so cancel() can later be called on it.
return { cancel }
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);
};
setTimeout(wrapper, nextAt - new Date().getTime());
return {
cancel: cancel
};
};
// Use like setTimeout
var timer = accurateInterval(function() {
console.log('message you will see every second!');
}, 1000);
// Or provide the time first and callback 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 on the returned object.
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.
@pred1980
Copy link
Copy Markdown

Thanks. I'll test it, because i noticed that setInterval often works not correct.

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