Muh*_*man 12 javascript jquery callback
这是我的代码
function save_current_side(current_side) {
var result;
var final = a.b({
callback: function (a) {
console.log(a); // its working fine here
return a;
}
});
}
Run Code Online (Sandbox Code Playgroud)
其中b是同步函数.我在代码中的任何地方调用上面的函数
var saved = save_current_side(current_side);
Run Code Online (Sandbox Code Playgroud)
保存的变量未定义.如何通过回调函数返回值
Guf*_*ffa 25
如果b是一个synchronoys方法,你只需将值存储在一个变量中,这样就可以从save_current_side函数而不是回调函数中返回它:
function save_current_side(current_side) {
var result;
a.b({
callback: function (a) {
result = a;
}
});
return result;
}
Run Code Online (Sandbox Code Playgroud)
如果b是异步方法,则无法从函数返回值,因为退出函数时它仍然不存在.使用回调:
function save_current_side(current_side, callback) {
a.b({
callback: function (a) {
callback(a);
}
});
}
save_current_side(current_side, function(a){
console.log(a);
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
54423 次 |
| 最近记录: |