Replace setTimeout with request animation frame