OZONE Asylum
Forums
DHTML/Javascript
Asynchronous For-In Loops in Firefox
This page's ID:
30978
Search
QuickChanges
Forums
FAQ
Archives
Register
Edit Post
Who can edit a post?
The poster and administrators may edit a post. The poster can only edit it for a short while after the initial post.
Your User Name:
Your Password:
Login Options:
Remember Me On This Computer
Your Text:
Insert Slimies »
Insert UBB Code »
Close
Last Tag
|
All Tags
UBB Help
This isn't cross-browser, but it is interesting nonetheless, and in any case, I haven't seen it posted anywhere else... Or mentioned anywhere else... Or listed in Google... Anyway, it's possible to write an asynchronous for-in loop using a new feature of JavaScript 1.7: a FF2+ object called Iterator. [url=https://developer.mozilla.org/en/New_in_JavaScript_1.7#Iterators]Click here to read about how it works.[/url] Anyway, here's a code for an object-based, Iterator-using, self-contained asynchronous looping mechanism. [code]AsyncLooper = { go: function(obj,iCallback,eCallback) { AsyncLooper.loop(obj,iCallback,eCallback,Iterator(obj)); }, loop: function(o,i,e,I) { var prop; for(var counter=0;counter<21;counter++) { try { // only way to check if there aren't any more properties prop=I.next(); } catch (e) { // if no more properties return e(o) } i(o,p[0],p[1]); } window.setTimeout(AsyncLooper.loop,1,o,i,e,I); } }; // And a usage example: myPointlessObject = {a:123,b:456,c:789,e:345,d:012}; STRING="myPointlessObject == "+myPointlessObject.toSource(); AsyncLooper.go(myPointlessObject, function(o,n,v) { STRING+="\nmyPointlessObject['"+n+"'] == "+v }, function() { alert(STRING); } );[/code] To use it, you would call [b]AsyncLooper.go()[/b] with the following arguments: [b]obj[/b] == The object to loop through. [b]iCallback[/b] == A function to be called on each iteration. It receives the object, property name, and property value as arguments. [b]eCallback[/b] == A function to be called when the loop ends. It receives the object as an argument. And there you have it: an asynchronous for-in loop. As a side note, it's possible to iterate through an Iterator using a for-in loop, but breaking out of the for-in loop kills the iterator (future for-in loops on it won't run, and any future calls to its [b]next()[/b] method generate a StopException). [code]// Assume that myIterator is an Iterator. for(i in myIterator) { break; } for(i in myIterator) { // even if the break statement // above ran before the Iterator // was "finished", this code will // never run... } myIterator.next(); // and calls to its next() method // cause StopExceptions.[/code] ---------------------- [url=http://www.ozoneasylum.com/30926][img]http://img19.imageshack.us/img19/268/cellu.gif[/img][/url][url=http://davidjcobb.deviantart.com/][img]http://img262.imageshack.us/img262/428/24464470.gif[/img][/url][url=http://getfirefox.com/][img]http://img515.imageshack.us/img515/6336/25338231.gif[/img][/url][img]http://img4.imageshack.us/img4/2163/end.gif[/img]
Loading...
Options:
Enable Slimies
Enable Linkwords
« Backwards
—
Onwards »