0
私はFireBase、NOSQLクラウドデータストレージで作業しています。子(データ)が変更されたときにJavaScriptを自動的に更新することができます。しかし、私はそれについて問題があります。ajaxの遅延を処理する方法
AJAXのスクリプト:
var myDataRef = new Firebase('https://App_Name.firebaseio.com/session');
myDataRef.on('child_added', function(snapshot) {
var message = snapshot.val();
console.log("out of ajax: user"+ message.userId);
$.ajax({
type: 'POST',
url: "showProfile.php",
data: {userId : message.userId},
dataType: 'json',
success: function (data) {
console.log("inside ajax: user" + data.userId);
},
error: function(e) {
console.log(e);
}
});
});
問題1は、同期を実行していません。第二に、それは順不同です。 inside ajax:
の順序は、最初に来る順序に依存します。コンソールで
結果:
out of ajax: user1
out of ajax: user2
out of ajax: user3
out of ajax: user4
inside ajax: user3
inside ajax: user2
inside ajax: user4
inside ajax: user1
しかし、私の期待される結果は次のとおりです。
out of ajax: user1
inside ajax: user1
out of ajax: user2
inside ajax: user2
out of ajax: user3
inside ajax: user3
out of ajax: user4
inside ajax: user4
または
out of ajax: user1
out of ajax: user2
out of ajax: user3
out of ajax: user4
inside ajax: user1
inside ajax: user2
inside ajax: user3
inside ajax: user4
'$ .ajax'に' async:false'を指定すると、リクエストを同期させることができます – mani
なぜそれを同期させたいのですか? –
@mani私はもう一つの大きな問題を抱えています...もっと遅くなります...動かなくなります。 –