Unsure why variable is undefined. Possible scope issue?

前端 未结 4 638
夕颜
夕颜 2021-01-26 13:28

If you look at the function below, on line 11 where it alert(template);. It prints undefined. If I alert(template); inside the ajax succes

4条回答
  •  面向向阳花
    2021-01-26 13:52

    The success function is a callback. The javascript engine makes the request to the server, and when the server responds to the data, then it runs the success function and you have the data, so the success function is run after your if statement at the end, even though its defined beforehand.

    Javascript code is run inside of an Event Loop, all $.ajax is doing is starting an asynchronous event and specifying what to do when that event occurs.

提交回复
热议问题