You can use the code in "newtonscadlereal" to study how to process "event" in EJS.
you need to return negative for EJS know event occurs, and it can adjust the time step to find the exact time event really happened.  (Adjust time step so that the return value is minimum).