我尝试比较两个不同的+new Date
,但有时会给出不同的结果。
所以我就是这样做的:
第一个用户向 Firebase(服务器)发送请求并设置值
时间:+新日期
像这样
firebase.userRef.set({
time: +new Date
})
然后第二个用户向 Firebase 发送类似的请求并按上述方式设置值。然后第二个用户在查询中找到用户 1 并检查谁先发送了请求。就像这样:
firebase.usersRef.child(user_uid).once("value",function(snapshot){
firebase.usersRef.child(current_uid).once("value",function(childSnapshot) {
if(snapshot.val().time > childSnapshot.val().time){
//current user requested first
} else {
//user found requested first
}
});
});
但是,有时两个用户都会得到错误的答案。因此用户认为当前用户首先请求。
最佳答案
设置时间戳的更好方法是使用 ServerValues :
userRef.set({
time: firebase.database.ServerValue.TIMESTAMP
})
使用服务器值会更好,因为一旦到达 Firebase 的服务器就会获取该值,并且可以使您的比较准确。
关于javascript - 比较两个 +new 日期字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46157797/