Created
July 29, 2011 20:29
-
-
Save bryanforbes/1114664 to your computer and use it in GitHub Desktop.
poller
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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