jquery - javascript global variable not updating in ajax callback -


on alert alerts jojo. test2.php have text loco

the div gets updated loco global variable not changing. tried window.temp = data didn't worked.

how can returned value in variable? please guide...

<div> yolo </div>  <script type="text/javascript">     var temp = 'jojo';          $.ajax({             url: 'test2.php', //loco             success: function(data)             {                     temp = data;                     $('div').html(data);             },             error: function (err)              {                  alert('error');              }         });         alert(temp);        var tobeusedlater = temp; //updated temp value ajax call        refreshtab(); </script> 

this asynchronous function. (that's a in ajax stands for.) alerting value of temp , happening before asynchronous call has finished. if add

alert(temp); 

to end of success handler, see value has updated.


Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -