Introducing PollJS – Javascript timers made easy.
One thing that has always caused issue when writing web applications is the issue of querying the server for long-running background tasks (generating thumbnails or PDF's, sending batch emails, etc). Often times these actions are too long-running to be contained in an HTTP request, so a short request is made and an interval is set up to begin querying for the eventual finish. This isn't a terrible way to do things, but the current implementation was lacking. For example, to kill the interval you need to assign the returned value of your "setInterval" call to a variable, and clear it out with "clearInterval". This raises the problem of variable scope. Now you need to go up and up and up, and declare a global variable to contain this interval, simply so you can kill it later.
But what if that long running job never gets executed during this particular session? And what if you have many different operations that need this type of implementation? You've now got your global namespace polluted with oft-unused variables. That's where PollJS steps in.
PollJS is a convenience wrapper for the native "setTimeout", "setInterval", "clearTimeout", and "clearInterval" methods. It wraps them with a simple definition method, and extends their functionality with additional options:
// A Basic Example
Poll.start({
"update_users",
interval: 1500,
action: function(){
alert("Updated!");
}
});
Poll.stop("update_users");
You can read more about the project, and download the source code on the Github page.