I'm creating a long list of select items and wanted to create a closure call to add each option, then queue the call (via a setTimer) so the browser would not hang.
My implementation works great, but here is what has me puzzled, the following code:
var mainList = $('#mainList');
for (var i=0;i < 100000; i++) {
var self = mainList, addOption = function() {
$(self).append('<option value=' + i + '>' + i + '</option>');
};
$.queue.add(addOption, this);
}
generates:
<option value='100000'>100000</option>
<option value='100000'>100000</option>
<option value='100000'>100000</option> etc...
Where I would like to have it generate the options:
<option value='1'>1</option>
<option value='2'>2</option>
<option value='3'>3</option> etc...
I'm struggling with how the closure is executed, it makes sense that the addOption() method is called when i == 100000, but I would like the call to reflect the value of i at the time it is queued up.
Is there an easy trick I am missing here?