简体   繁体   中英

global variable stays empty

I have a piece of javascript code where I retrieve a list of employees via an ajax-call.

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)

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?
Or have I made a type I don't see!?!

your console.log is executed before the 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]

javascript is 1 thread , yet you never know when the callback function will run. so you have to adjust your code .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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