javascript - How to pass data by value into a deferred then handler? -


i know how pass data value, rather reference, then handler on jquery deferred object.

i have following example code illustrate question:

var value; var deferred = new $.deferred(); var data = ["a", "b", "c", "d"];  // add separate call chain each piece of data (var = 0; < data.length; i++) {   value = data[i];   deferred.then(function(response) {     console.log(response+':'+value);   }); }  deferred.resolve("test"); 

the result want get:

test:a test:b test:c test:d 

the result get:

test:d test:d test:d test:d 

it seems value of value evaluated @ time then handler executed, whereas want evaluated @ time then handler queued.

i have jsfiddle, hope can help?

yes.. have create closure around it

for (var = 0; < data.length; i++) {     value = data[i];     deferred.then( function(val) {         return function(response) {             console.log(response+':'+val);         }}(value)); } 

Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -