简体   繁体   English

全局变量保持为空

[英]global variable stays empty

I have a piece of javascript code where I retrieve a list of employees via an ajax-call. 我有一段JavaScript代码,可通过ajax调用检索员工列表。

var employees_json = '';

Ajax.callbackAsync('getEmployees', ['2627016'],
    function(response){
        employees_json = response;

        console.log('this: '+ employees_json);
    },
    function(error){
        alert('Error while retrieving employees: '+ error);
    }
);

console.log('that: '+ employees_json);

(2627016 is the branch_id) (2627016是branch_id)

the output on the console: 控制台上的输出:

this: [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
that: 

Why is employees_json empty outside the ajax-call? 为什么ajax调用之外的employee_json为空?
Or have I made a type I don't see!?! 还是我做了一个看不见的文字!?!

your console.log is executed before the function(response) . 您的console.log在function(response)之前执行。

log is not like this ? 日志不是这样吗? : that: this: [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object] :that:this:[对象对象],[对象对象],[对象对象],[对象对象],[对象对象],[对象对象],[对象对象],[对象对象],[对象对象], [对象对象],[对象对象],[对象对象]

javascript is 1 thread , yet you never know when the callback function will run. javascript是1个线程,但您永远不知道回调函数何时运行。 so you have to adjust your code . 所以你必须调整你的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM