I
I
iffkomc2015-10-02 12:50:58
JavaScript
iffkomc, 2015-10-02 12:50:58

Why is the variable after the ajax request set to undefined?

Guys, I sat down to write code and came across some kind of nonsense I didn’t understand) This is probably one of the js jokes. Please explain. Declared a global empty variable. when I use it in a function, a local variable is automatically created and the values ​​are written to it, and not to the global one. How to fix it?
jsfiddle.net/iffkomc/ro3hmxpa

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ukolov, 2015-10-02
@iffkomc

when I use it in a function, a local variable is automatically created and values ​​are written to it, and not to the global one
This is not true. Scopes have nothing to do with it, the point is asynchrony - alert is executed before the request is completed and at this moment the user data has not yet been received.
You here: How to make so that the function is executed only after the other is completed?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question