what is the elegant way to make EJS measure a true period of an oscillation?

I want to make a pendulum to measure the true period.

i tried

if ( omega < 0.001 & omega > -0.001 ) {  // my inital idea how to detect the end points of the oscillation
  count = count+1;                              // make count increase by 1 if condition met
}


if (  count ==2 ) {              // count =2
  T1 = t ;                                      // T1 = t
}

it doesn't work.

any tips work to do it?