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
Post a Comment