Colorfulstan
9/6/2015 - 7:15 PM

passing callbacks using a loop-iteration counter

passing callbacks using a loop-iteration counter

function create_callback(i){
			return function callback(callbackParams) {
				// do something with the "right" i
			}
		}
		
		// create closures with the temporary i and store them for later
		var funcs = [];
		for (var i = 0; i < 5; i++) {
			funcs[i] = create_callback(i);
		}
		
		// give the stored closures with encapsulated i as callbacks
		for (var j = 0; j < 5; j++) {
			giveMeACallback( funcs[j] );
		}