Topic: Asynchronous For-In Loops in Firefox |
|
|---|---|
| Author | Thread |
|
Nervous Wreck (II) Inmate From: United States |
posted 04-29-2009 21:26
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... 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: // 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. |
|
Nervous Wreck (II) Inmate From: |
posted 05-31-2011 11:08
Edit TP: spam removed
|