Using setInterval instead of while loop to iterate