I want to access returned variable from one ajax call in another ajax call and want to repeat the second ajax call continuously. my code is below, and the required variables are mentioned in code comments.
<script type="text/javascript" language="javascript">
$( document ).ready(function() {
$.ajax({
url : "functions.php?id=enter_game&name=<?php echo $name; ?>",
type : "GET",
//dataType : 'json',
success: function(result){
if(result){
$("#game_status").html(result["game"]);
var limit = Object.keys(result).length - 4;
for(var x = 1; x <= limit ; x++)
{
$("#users").append('<li>'+result[x]["name"]+'</li>');
//$("#users").append('<li>abd</li>');
}
// I want to access these variables in next ajax call
var user_id = result["current_user_id"];
var word_id = result["word_id"];
}
}
});
// start updating continuously
var timer, delay = 1000; // time in milli seconds
timer = setInterval(function(){
$.ajax({
type : 'GET',
// the variables from previous ajax call result should be avaialable here
// and only this ajax call should be made upon delay time.
// previous ajax call should not be made more than once.
url : 'functions.php?user_id='+user_id+'&word_id='+word_id,
//dataType: 'json',
success : function(data){
if(data){
$("#game_status").html(Math.floor((Math.random() * 10) + 1));
}
},
error: function( xhr, status, errorThrown ) {
alert( "Sorry, there was a problem! Error: " + errorThrown + ", Status: " + status + ", xhr: ," + xhr);
console.log( "Error: " + errorThrown );
console.log( "Status: " + status );
console.dir( xhr );
}
});
}, delay);
});
</script>
Any help would be much appreciated.