Reputation: 1
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
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