Re: how to walk async recursively over an object, doing work (in right order) per leaf

From: Date: Wed, 13 Jun 2012 21:49:43 +0000
Subject: Re: how to walk async recursively over an object, doing work (in right order) per leaf
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-318230@lists.php.net to get a copy of this message
On Wed, Jun 13, 2012 at 7:03 PM, rene7705 <rene7705@gmail.com> wrote: > I'm a bit stumped on a javascript problem at the moment, I hope you > don't mind (too much) that I post it here as well, for a wider > audience.. > > My opensourced htmlMicroscope works well, except when you open a > sub-array that holds more than say a hundred keys. Maybe 200. > > But the thing is, I'd like it to work on all kinds of arrays, > including ones that have 1500 keys in some sub-object/array somewhere. > > Like for instance my serviceLog now does when viewing the hits per > month (not per day). > > And I'm sure other applications that might use my htmlMicroscope will > suffer suddenly from the same problem, forcing the end-user of hm() to > mold his data at unexpected turns, and sometimes against his/her > wishes. > > So I'd like to solve this issue. Please help me out here, I've been > stuck on it for the better part of a day now.. > > This loop needs to be a-synchronized; > > function printLevel : function (data) { >  var html = ''; >  if (typeof data=='object' && data!==null && data!==undefined) { >    for (var k in data) { >      // and the problem is you'd have up to several thousand var k's. > the loop needs to setTimeout after say a hundred iterations through > THIS loop... >      var v = data[k]; >      html += > '<tr><td>'+printVariable(k)+'</td><td>'+printVariable > (v)+'</td></tr>'; >    } >  } >  return html; > } > > function printVariable : function (data) { >   var t = typeof data; >   switch (t) { >     case 'number' : >     case 'string' : >     case 'boolean': >           return data; >           break; >     case 'object': >         return printLevel (data); >         break; >   } > } > Try something like this: var data; var index = 0; function printLevel : function() { for(i = 0; i < index + 100 && data.length; i++) { // Calculate stuff here } index += 100; // Call setTimeout here } It's untested, but I think this should work, or atleast work with a few modifications - Matijn

« previous php.general (#318230) next »