refactor and setinterval for linear search