javascript snippet :
<script type="text/javascript">
$(document).ready(function() {
setInterval(function() {
$.getJSON('notif.php', function(data) {
my_var = '123';
});
alert("my var =" + my_var);
}, 2000);
});
</script>
in chrome and FF console, my_var shows to be undefined, but its value is shown on the alertbox. if undefined , then how is its val
What is the explanation ?
getJSON in jquery is by default
asynchcall so thealert("my var =" + my_var);will be executed without waiting for the response of the ajax call.move the alert inside callback function like