Scope variable in ajax call

前端 未结 3 1294
一生所求
一生所求 2021-01-14 17:57

Why the final console log is undefined?Variable time has a global scope and ajax call is async.

This is my code:

var time;
$.ajax({
    async: false,         


        
3条回答
  •  迷失自我
    2021-01-14 18:28

    In your code, you initialize the global variable 'time' as 'data'. Where does this data variable come from? If the data variable is not global also, when you try to use console.log(time); , it may be undefined because the data variable is undefined.

    Make sure both variables are within scope to be used globally. That might work. Good luck!

提交回复
热议问题