jQuery的AJAX成功回调的范围有多大?
问题描述:
如果我有
function AjaxRequest(){
var testvar = 0;
for(i=0;i<d.length;i++){
$.ajax({
success: function(a){
testvar++;
}
});
}
}
将增加的testvar成功?
Will testvar increase on success?
答
是的;该变量是由函数的闭包捕获。
闭包保持变量活着,这样嵌套函数仍然可以在以后使用它们。
Yes; the variable is captured by the function's closure.
Closures keep variables alive so that nested functions can still use them later.
注意成功
只回调的code完成(AJAX是异步的)。
Note that the success
callbacks only run some time after the rest of your code finishes (AJAX is asynchronous).