To avoid churn, you should keep track of the timeout ID and cancel any previous timeout before setting the new one. (Otherwise, if the user rapidly switches between files within the 500ms interval, the browser will thrash between them as well.)