Generalized infinite scrolling function.