secret
Last active

  • Download Gist
accurate_interval.coffee
CoffeeScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
# 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 }
accurate_interval.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
(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);
accurate_interval_usage.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12
// 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();
mit.license
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
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.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.