A stupidly-simple "chasing" algoritm in JavaScript