Skip to content

Instantly share code, notes, and snippets.

@bryanforbes
Created July 29, 2011 20:29
Show Gist options
  • Save bryanforbes/1114664 to your computer and use it in GitHub Desktop.
Save bryanforbes/1114664 to your computer and use it in GitHub Desktop.
poller
function poller(callback, delay){
var polling, timeout;
function set(time){
timeout = setTimeout(function(){
// if a timeout is already scheduled in IE,
// clearTimeout won't remove it from the schedule
if(!polling || polling != time){ return; }
callback();
set(time);
}, delay);
}
function start(){
set(polling = +(new Date));
}
start();
return {
pause: function(){
polling = 0;
clearTimeout(timeout);
timeout = null;
},
resume: function(){
if(!polling){
start();
}
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment