I”m getting input as a stream of x/y coords, and I’d like to trigger an event if those coords fall within a specified range for a given period of time (say 2 seconds). Could someone suggest a way to do this?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Algorithmically, when you first detect that it’s in the range, you do a
setTimeout()for 2 seconds. From then on, if the coords stay in the bounds and the timer is already set, you do nothing. If the coords fall outside the bounds, you stop the timer withclearTimeout(). If the timer fires before you’ve cleared it, then the coords have stayed in the bounds for the desired time.Here’s a code example:
The implementation of the
inBounds()function is obviously up to you since you haven’t described that part of the problem.