Syntax solutions
19 Dec 2011
For loops with Turboid loop
Simulating for loops without freezing the browser
This loop feature is more powerful as it seems.
But it is not only a compensatory for while loops to avoid freezing the browser, but also suitable as a for loop replacement.
The Turboid loop is an awesome feature of Turboid, as you can implement a loop like this one without freezing the browser:
while(true){ // Do something... }... by simply using this short syntax instead of the a little bit long-winded and sometimes because of other reasons annoying window.setInterval()-function:
loop(function(){ // Do something... });
This loop feature is more powerful as it seems at the first glance (learn more in this article: "Real loops in JavaScript"). But it is not only a compensatory for while loops, but also for for loops. If you want to implement with loop() an alternative for a for loop like this one...
for(var i = 0; i < 35; i++){ alert(i); }
... then this is the loop() version of it:
var i=0; loop(function(){ alert(i); i++; }, 1, 35);
However it only works that way if the loop() call is part of a function. Otherwise the index should be an object property, but that is no big deal:
obj = {}; obj.i= 0; loop(function(){ alert(obj.i); obj.i++; }, 1, 35);Have fun!
Comments
Add comment: