Beaast_exe
Beaast_exe

Reputation: 1

How can i run some code at an exact timestamp?

I have a REST API in a backend with an array of objects with a timestamp (time when something happens in-game) and a value.

{"timestamp":1623320102097,"crops":[0,5,9]}

How can i do run something when time is equals to that timestamp ?

Upvotes: 0

Views: 41

Answers (1)

slkorolev
slkorolev

Reputation: 6001

I presume that timestamps are in msec. Probably this should do the trick:

let diffMsec = obj.timestamp - new Date().getTime();
if (diffMsec > 0) {
   setTimeout(function() {
      /* do your stuff */
   }, diffMsec);
}

Keep in mind though, that it is not guaranteed that timeout will be invoked at the exact time.

Upvotes: 2

Related Questions