[turboid] javascript solutions

Home     Contact     Search     About me
Syntax solutions
11 Jan 2012

Synchronizing and unifying intervals

Order instead of chaos

Want to save performance instead of using a chaos of dozens of separately working intervals? A new Turboid function makes this possible. Every time you use it, you extend the interval by your new steps, which would be executed by the same loop and at the same time as the other functions it has been extended by before.

In complex web applications based on JavaScript it may be necessary to use a whole bunch of intervals (or better: Turboid loops) started at different points of time. This can lead to performance problems if you have for example 50 intervals (or Turboid loops), each one started at a different point of time, and each one working seperately.

Thus it would be way better to have only one interval which gets extended with the respective functions whenever this is needed, without starting a new separate interval.

Exactly this is possible with our free Turboid framework (respectively its small core), if you use the function loop.timer feature. Instead of declaring a new setInterval call you write:

loop.timer.add(function(){
	// Do something
});

Every time you use this function, you extend the interval by your new steps, which would be executed by the same loop and at the same time as the other functions it has been extended by before. That one loop operates with an interval length of 50 milliseconds. This is the only disadvantage compared with separately started intervals, i.e. you can't choose your interval freely - except that you can tell the loop.timer not to call your function every 50 milliseconds, but every 1000 milliseconds:

loop.timer.addSlow(function(){
	// Do something
});

However, this is a good method for constructing your own event handlers in case they can't be derived from existing event handlers.

You can also drop a function from the synchronized function queue, either per index - here the fifth one will be dropped - :

loop.timer.drop(function(){
	// Do something
}, 4);
... or per label:
loop.timer.drop(function(){
	// Do something
}, "myTimerExtension123");

But dropping per label requires that you have already given your extension a label this way:

loop.timer.add(function(){
	// Do something
}, "myTimerExtension123");

However, beware of extending the timer by functions containing alert functions or other things which slowdown the timer - this could interfer with the other synchronized functions.

In order to use this technique, simply download the free Turboid framework and put it into the directory of your HTML file, then write this line into the head section of your HTML file, and that's it:

	

Alternatively, you can use the smaller Turboid core if you don't want to download the whole framework.

	
I hope this has been useful for you!

Comments

Add comment:

Name: (required)
E-Mail: (required, remains invisible)
Website:
Comment:
Please check all (anti-spam):