Skip to content

Instantly share code, notes, and snippets.

@anthonybrown
Forked from maccman/jquery.wake.coffee
Created November 12, 2013 20:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anthonybrown/7438014 to your computer and use it in GitHub Desktop.
Save anthonybrown/7438014 to your computer and use it in GitHub Desktop.
$ = jQuery
TIMEOUT = 20000
lastTime = (new Date()).getTime()
setInterval ->
currentTime = (new Date()).getTime()
# If timeout was paused (ignoring small
# variations) then trigger the 'wake' event
if currentTime > (lastTime + TIMEOUT + 2000)
$(document).wake()
lastTime = currentTime
, TIMEOUT
$.fn.wake = (callback) ->
if typeof callback is 'function'
$(this).on('wake', callback)
else
$(this).trigger('wake', arguments...)
(function($){
var TIMEOUT = 20000;
var lastTime = (new Date()).getTime();
setInterval(function() {
var currentTime = (new Date()).getTime();
if (currentTime > (lastTime + TIMEOUT + 2000)) {
$(document).wake();
}
lastTime = currentTime;
}, TIMEOUT);
$.fn.wake = function(callback) {
if (typeof callback === 'function') {
return $(this).on('wake', callback);
} else {
return $(this).trigger('wake');
}
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment