Interval Manager

I am a big fan of the Lazy Function Pattern, however, yesterday I realized a serious pitfall that you can encounter when using this pattern with functions that need to be executed in a certain order. Assume for a minute that you have an object requiring an AJAX request to initialize its content, but when you instantiate the object, you immediately want to manipulate it. For example, you have an Object DomModule, with two public functions: show and update. When you instantiate the DomModule object, it fires an AJAX request to update itself, then the code instantiating DomModule immediately calls "DomModule.update" and "". Both functions require that the DOM node be set, which happens when the AJAX returns, before executing, so each function has the following code snippet:

Example 1: Lazy-Loading Snippet

if (! that.node) { // node is set by AJAX
	setTimeout(function() {;}, 500);

This works great except when we need to guarantee that the "DomModule.update" occurs before "". Since both have unrelated timeouts the behavior is non-deterministic. We can add special logic each time we need deterministic order for lazy-loaded functions, however that is less than ideal. It would be better if we could use general logic to specify order priority to our timeouts. For that reason I have written a beta Interval object that manages priority, number of executions, execution limits, and more. Also, it may be possible to game some performance improvement or more deterministic times over the native JavaScript setTimeout and setInterval by managing one instance of the interval, instead of relying on the browser.

The code is still in beta and rather than attempt to walk through, I will summarize its features and provide a demo page for those interested with exploring. The Interval object can handle any interval divisible by 25ms, or will covert the interval to the nearest multiple of 25. You can provide a stop function that will terminate the interval when a certain criteria is reached. In addition, you can also specify the max number of times an interval callback can be executed and its priority from 1 through 10, where lower priority executes first, and if the priority is tied, then the execution order is the order that the interval callbacks were passed into the Interval object. Lastly, when the callback function is executed, it will be passed an object representing the current state of the interval.

When setting up an interval, use the following:

Example 2: Starting A New Interval

Interval.setInterval(callbackFunction, stopFunction, timeoutInterval, priority, maximumNumberofExecutions);

If we look back at our original example, we can now use the Interval object to set a priority to our lazy functions, so that "DomModule.update" callback occurs before the "". No special logic is required and Interval can be used to manage other page intervals, whether they need priority or not.

The only required value for setInterval is the callbackFunction, everything else can be undefined. Right now, I would not recommend using any timeoutInterval shorter than 100ms as the code execution starts to affect the interval time. I have also noticed that the performance tends to degrade when adding 10+ intervals at a given time. I have attempted to optimize, but there is still room for improvement. The source code Interval.js is available here and a test page here