变量范围Javascript模块模式

时间:2015-08-09 15:26:54

标签: javascript scope

我有以下代码:

select i.invId,i.invAmt,ifnull(sum(p.amt),0) as paid,i.invAmt-ifnull(sum(p.amt),0) as due
from invoice i
left join payment p
on p.invId=i.invId
where i.custId=101
group by i.invId
having due<0
order by i.invId

来自Main.User = (function(){ var currentUser = '' var get_current_user = function get_current_user(){ Main.Mod.do_ajax('home/get_user_pnp_id','GET',function(data){ currentUser = data.username; },null); console.log(currentUser); //Doesn't work. It logs empty string. return currentUser; } return { get_current_user : get_current_user, } })(); 的第3个参数是来自我的ajax调用的回调成功函数。但是我无法在该回调函数中设置Main.Mod.do_ajax()

什么似乎是问题?

1 个答案:

答案 0 :(得分:1)

这是因为由于回调是异步的,所以在进行分配时还没有调用回调。请参阅Ajax jquery async return valuehttp://node-tricks.com/return-response-ajax-call/