Skip to content

Instantly share code, notes, and snippets.

@damian
Created May 20, 2010 14:07
Show Gist options
  • Save damian/407588 to your computer and use it in GitHub Desktop.
Save damian/407588 to your computer and use it in GitHub Desktop.
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">
function Async(options){};
Async.each=function(list,callback){
var _count=0;
if(list.length==null){
_list=[];
$.each(list,function(i,n){
_list.push({
"index": i,
"item": n
});
});
list=_list;
}
function onto_next(){
if (_count >= list.length) {
callback(null);
}
else {
callback({
index: _count,
item: list[_count],
callback: function(){
_count++;
onto_next();
}
});
}
};
onto_next();
};
Async.multi_thread_load=function(list,callback){
Async.each(list,function(each){
if(each){
each.item(function(e){
each.callback();
});
}else{
callback();
}
});
};
</script>
<ul id="list">
<li>First</li>
</ul>
<script type="text/javascript" charset="utf-8">
var list = $('#list');
var delegates = [];
for (var i=0; i < 10; i++) {
delegates.push(function(ap_stuff){
$.ajax({
url: 'test_1.html',
dataType: 'html',
success:function(data){
ap_stuff();
}
});
});
}
function ap_stuff() {
list.append('<li>hi</li>');
}
function on_complete() {
// Carry on doing what you want to do!
}
Async.multi_thread_load(delegates,on_complete);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment