Hrtime in ionic


#1

The touchstart and touchend events in Ionic currently return a timeStamp which is not Date.now(), but (seeing the value and its sub-ms resolution) it is I’d think based on process.hrtime() or similar (equivalent to performance.now() in the browser).
My question is: how can I simply call this exact same timestamp (without the events) so that I would have a base comparison to the event timestamps?

I’d use it e.g. like this:

start = now(); // this "now()" is what I'm looking for
// things happen
// (touchstart)="touchstart($event);" is triggered
touchstart(event) {
        time_elapsed = event.timeStamp - start;
}

By the way I tried and I can actually call the process variable, but it has no hrtime property.


#2

What r u trying to achieve?

Debounce input from user?


#3

no. it’s a reaction-time task and I’d need to store the “time_elapsed” variable from the example (more precisely: the time between the appearance of one element and the touchstart event)


#4

Ok
That is indeed a different case then I had in mind

For measuring time I read there are multiple ways

But this u may alreasy know

What I would try is to measure the difference between event-time and my own time and use this calibration to calculate the actual response time in the reaction game

Not sure if this helps

Alternative to using the event data is to handle the reaction time through the (click) event handler itself

Regards

Tom


#5

Thanks for the tips, but yeah, this I knew. But performance.now() cannot be simply called from Ioinic (as far as I know, but tell me if there is a way), and I also tried a related nodejs package (https://www.npmjs.com/package/performance-now) but had trouble using it with Ionic. Of course in the worst case I can always resort to Date.now() and just call it in the event handler function as you said, but I’m still looking for a better way (using the timeStamp and hrtime).

But still, what I don’t get is how the touchstart gets its timeStamp value. If it can call it then why can’t I in my code? I even tried to go through the ionic-3 source code but all I see is Date.now() everywhere. So either it’s outdated or I’m missing something.


#6

Hi
don’t see why performance.now() cannot be called plain vanilla without packages

Check the constructor in home.ts

But that doesn’t answer your valid question on the timeStamp implementation in Ionic. Makes me curious too…

Update: the github repo on ionic is now in Ionic 4 code. But here you see:

export function now(ev: UIEvent) {
  return ev.timeStamp || Date.now();
}

In https://github.com/ionic-team/ionic/blob/1bbd73cadafee784d48dbd96f9e225d8b35f2596/core/src/utils/helpers.ts

Called by for instance by https://github.com/ionic-team/ionic/blob/1bbd73cadafee784d48dbd96f9e225d8b35f2596/core/src/components/ripple-effect/ripple-effect.tsx (search for touchstart)

Which seems to indicate it either uses the timeStamp generated by the browser (assuming that is the argument passed) or generate one using Date.nowI().


#7

You are totally right, performance.now() does work for me too, I have no clue what I did wrong previously that it didn’t. And I will simply use that as a baseline since it seems equivalent to touchstart’s (or UIEvent’s) timeStamp:

Thanks very much for the help.