【发布时间】:2013-01-18 14:58:01
【问题描述】:
这真的让我着迷:
for(var i=0; i<10; i++) {
(function(x) { //use a closure to hold the "i" value
request(arg[x], function(n) {
//do something with the data returned from $getJSON
console.log(n);
});
})(i) //is this syntax correct?
}
function request(argX, callback) { //is this syntax correct?
$getJSON(parameter) {
//get request result
...
}
callback(); //after request() function is completed, trigger the callback function
//is this syntax right?
}
我之所以使用回调函数是因为我想操作getJSON的结果,所以我需要等待请求函数完成。
我还需要将回调函数与循环索引“i”值绑定。
我已经玩了一段时间的语法,但是控制台日志为什么没有返回任何内容?在 $getJSON 完成之前,似乎从未执行或执行过回调函数。
我需要专家的帮助!
【问题讨论】:
-
"在 $getJSON 完成之前似乎从未执行或执行过回调函数。" - - 正确的。因为
$.getJSON是异步的 -
request(arg[x], function(n) { 缺少右括号 ')' 否?