ajax - Make javascript execute linearly -
I am making a game when a piece ('.draggable') is taken, then let me first call AJAX call (move_script .php), which will validate the move and then save it to the database. After that, I want to run the second AJAX call (past_moves.php) and inquire about the database and write a new move. My problem is, that sometimes the second AJAX call pulls the results before it is the current trick. I have used microtam (); In the PHP code to see what is happening, and every time the recent results are not removed, the second is completing the Ajax script first (most recent results appear, but every time this will not happen often, It seems random, every 5-15 times or so)
Code:
& lt; How can I ensure that the second ajax call will not be done before? script & gt; $ (Function () ($ (".draggable") .dragable ({stop: function (event, ui) {var start = $ (this) .attr ('id') // ajax call number 1 $ .ajax ( {Type: "POST", url: "../move_script.php", data: {start: start}}) done (function (msg) {// warning ("data saved:" + msg); $ ( "#inchboard"). Html (msg);}); // Ajax call no.2 $ .ajax ({type: "post", url: "../past_moves.php", data: {}}) ( Functions (moves) {$ ("# past_moves") .HTML (move);}}}}}})}};
Nested Ajax call
// ajax call no. 1 $ .ajax ({type: "POST", url : "../move_script.php", data: {start: start} }) Done (function (msg) {// warning ("data saved:" msg "; $ (" # chess "). Html (msg); // ajax call no. 2 $ .ajax ({type:" POST ", Url:" ../past_moves.php ", data: {}}. (Function (moves) {$ (" # past_moves "). Html (moves);})}}};}); This will force the nesting victim to wait until the first completion of the second call.
Comments
Post a Comment