Obtaining the Unix timestamp within Firebase: A step-by-step guide

Within my chess application, I utilize Firebase.ServerValue.TIMESTAMP to document the timing of each move.

If a user fails to make a move within 24 hours, they forfeit the game. It is important for me to indicate how much time the user has left to make their move. To achieve this, I must have access to the current unix timestamp and then execute the following calculation:

time left = (last move unix timestamp) + (24 hours) - (current unix timestamp)

Relying on the accuracy of the user's machine time may not be the most reliable approach.

Is there a method to attain the unix timestamp directly from Firebase servers?

Related inquiry

Answer №1

One way to determine the time difference between local and server is by using the method .info/serverTimeOffset:

var calculateTimeDifference = (function() {
  var OFFSET = 0;

  fbUtility.reference("/.info/serverTimeOffset").on('value', function(snapshot) {
    OFFSET = snapshot.val()||0;
  });

  return function() {
     return Date.now() + OFFSET;
  }
})();

var currentLocalTime = calculateTimeDifference();
var timeDelta = currentLocalTime - timestamp;

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What causes json.parse to malfunction? and how can you resolve the issue

My server sends data to my JavaScript code in the format below. {"triggers": [{"message_type": "sms","recipients": "[\"+91xxxxxxxxx\",\"+91xxxxxxxxx\"]", "message": "This is a test"}]} To parse this JSON string, my code executes the f ...

Incorporate Data into Table Rows inside the Material-UI Accordion

I am tasked with creating a table filled with two mock data accordions and one accordion containing database data using ReactJS in Material-UI. I have successfully implemented an accordion with the table data, but I am struggling to display only the table ...

The property 'body' cannot be read because it is undefined

I have been attempting to incorporate passport logic into my controllers file, but I encountered an issue. When I place the logic inside the controllers, it gives me an error message saying "Cannot read property 'body' of undefined." However, whe ...

The ngBindHtml directive is failing to display sanitized HTML content

I am encountering an issue with my Angular app where I am trying to insert HTML into the page using ngBindHtml, but it does not seem to be working. Take a look at my code below: <main id="main" ng-bind-html="oreGen | trust"></main> Here is my ...

A process of associating a single object with an array containing numerous objects

Can someone help me convert the following data structure? { role:'admin', resource:['calendar','appraisal'], action:['create:any','read:any','update:any','delete:any'] } { role:&apos ...

ngFor returning undefined despite array containing value

While iterating through an array using ngFor, I'm encountering an issue where trying to access data in the 'value' variable results in it being undefined. Why is this happening? myArray = ['a', 'b', 'c', 'd ...

The use of `preventDefault()` in React for the `onCopy` event is ineffective

I'm currently exploring ways to prevent the clipboard events from copying text when the onCopy event is triggered. I've tried using the onCopy handler and e.preventDefault() method, but the text is still being copied without any issues. What am I ...

ng-model establishes a connection with objects, not properties

Having just started my journey with AngularJS and JavaScript, I decided to create a simple app that allows users to input their name and age, and then displays the list of users and their ages. Here is the code I put together: var main = angular.module( ...

Troubleshooting NodeJS CORS issue with heavy requests for file uploads

I'm currently working on a project that involves an Angular front end and a NodeJS API deployed in production using AWS services like S3 and Elastic Beanstalk. When attempting to upload images, I encounter a CORS error if the image is too large or wh ...

Performing an action once all AJAX requests have finished

I'm dealing with a situation where I have a click event triggering 3 ajax calls: $("#button").click(function(e) { e.preventDefault(); $.ajax(call1); $.ajax(call2); $.ajax(call3); some_function() //needs to be executed only afte ...

Current recommended method for updating innerHTML with properly formatted text?

My understanding is that using innerHTML on an element can be risky, as malicious users could potentially inject harmful content such as <script> tags. This question shows there are multiple alternative tags, some of which are non-standard. In my s ...

Encountering difficulties accessing props while invoking a component in React

In my project, I've created a component called FilterSliders using Material UI. Within this component, I passed a prop named {classes.title} by destructuring the props with const { classes }: any = this.props;. However, when I try to access this prop ...

Loading a local image using Jquery in an open dialog

Hello to all fellow stackoverflow users! I am currently facing an issue while trying to load a local jpeg file into a dialog box. The problem is that no matter what, the same table information keeps getting appended and reopened in the dialog box. I must h ...

Looking to develop a swipeable card feature similar to Gmail using React JS (excluding React Native)?

Looking to replicate the swipe card functionality of Gmail in React Js, After creating the UI and implementing swiping using UseGeature, I am facing an issue with smooth animation when hiding or showing buttons using UseEffect. There is a 20px movement be ...

Is it possible for JavaScript and PHP to be used to create an engaging online card game

Is it possible to develop an online card game using only JavaScript, PHP, and AJAX without the need for Flash? The game will involve each player having a deck of X cards (such as sixty) with unique abilities. The functionality of the game, such as drawing ...

A quick demonstration of the node.js console.clear() method in action with a straightforward code snippet

Can someone provide me with a clear example of how to use the console.clear() method from the console module in node.js? I've searched extensively on stackoverflow but haven't found anything useful. console.log("Content printed in file"); con ...

What is the best way to completely replace all HTML content, including tags and other elements?

I am looking to completely change or replace an entire HTML tag, not just its content: function updateLikes(image_id) { $.ajax({ url: 'functions/updateLikes.php', method: 'POST', data: { ...

Arranging elements within an outer array by the contents of their inner arrays

I need help organizing an array based on the alphabetical order of a specific value within the inner arrays. For example: I want to sort this array by the prefix "old," so old A, old B, etc. const array = [ { personName: "Vans", personTags: ["young", " ...

Separating buttons (two buttons switching on and off simultaneously) and displaying the corresponding button based on the data

My current project involves creating a registration page for specific courses. While I am able to display the information correctly, I am facing an issue with the ng-repeat function. The problem lies in all the Register buttons toggling incorrectly. Additi ...

mastering the nuances of jQuery event namespaces

Below are some code snippets to consider: $("#atc-button").bind("click.hctpAttach", function (e) { console.log("Add to cart button clicked.") }); <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> ...