【发布时间】:2010-12-29 08:03:27
【问题描述】:
当尝试从 ajax 回调函数设置变量时,我得到了不可预知的结果:
var logged_in=false;
function check_response(response,el) {
logged_in=true;
alert(logged_in); //outputs TRUE
}
makePOSTRequest("/is_logged.php", check_response); //when ready call check_response() with the XML response
alert(logged_in); //first time outputs FALSE;
alert(logged_in); //second time outputs TRUE;
- 我运行 POSTRequest 来检查我的用户是否已登录。
- POSTRequest 回调函数 check_response() 以解析响应并将变量 logged_in 设置为 TRUE。
- 接下来,当我尝试使用变量 logged_in 时,它是:第一次 FALSE;然后是真的...
你知道为什么会发生这种奇怪的行为吗?
【问题讨论】:
标签: javascript ajax xmlhttprequest global-variables scope