var cs; // 1 在外面声明一个变量 cs
AV.Cloud.httpRequest({ // 2 一个 http 请求
url: 'http://cn.bing.com/cnhp/coverstory',
params: {
mkt: 'zh-cn',
d: date
},
success: function (httpResponse) { // 3 这是成功时候的回调函数
cs = httpResponse.text; // 4 把 http 请求返回的内容给 cs 这个变量
},
error: function (httpResponse) {
console.error('Request failed with response code ' + httpResponse.status);
}
});
response.success(cs); // 5 在外面读取 cs 变量
为什么当第五步读取cs变量时读取的是第一步的cs,而看起来cs变量没有被第4步修改?
如果我将第5步这一行移到第4步的那一行后面,也就是在函数内,就可以读取到修改后的cs。
如何实现回调函数修改外部的变量并可以被外部读取?
JS小白,可能问得不好,请多指点,谢谢!
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
回调函数里是异步,在外面肯定是得不到的,除非把他设为同步的