Friday 9 December 2011

js async for, very cool

This is something nice. This code can create for interrupts for those long functions in ie, so you can tell the user what is happening :). You can run it in the fireBug's console to see what I mean :).


function async_for_each(object,cbk,limit_callback,end_cbk,limit,timeout,start){
    var l=object.length;
 limit = limit || Math.round(l/100);
 start = start || 0;
 timeout = timeout || 1;
 for(var i=start;ilimit){
   setTimeout(function(){
    async_for_each(object,cbk,limit_callback,end_cbk,limit,timeout,i)
   },timeout);
   limit_callback(i,l);
   return false;
  }else{
   cbk(i,object[i]);
  }
 }
 end_cbk?end_cbk():null;
 return true;
}

var a = [];
a.length = 1000;

async_for_each(a,function(){},function(i,l){console.log("locading %s/%s - %s%",i,l,Math.round(i*100/l))});

No comments:

Post a Comment