Possible Duplicate:
Javascript closure inside loops - simple practical example
Let's imagine you create some ajax requests in a for-loop like this:
$(function(){
    for(var i=0;i<10;i++){
      $.ajax({
        url : '/',
        success : function(){
          console.log('callback '+i+' fired!');
        }
      });
    }
});
Of course, the callback will log 'callback 10 fired' every time because the callbacks are executed asynchronous. The question is: how can you know which callback fired?
 
     
     
     
    