if I have
function AjaxRequest(){
var testvar = 0;
for(i=0;i<d.length;i++){
$.ajax({
success: function(a){
testvar++;
}
});
}
}
Will testvar increase on success?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Yes; the variable is captured by the function’s closure.
Closures keep variables alive so that nested functions can still use them later.
Note that the
successcallbacks only run some time after the rest of your code finishes (AJAX is asynchronous).