The problem with JavaScript is that while JavaScript is running, the entire page is locked and will not display anything. If you have a very long cycle, this explains your problem.
Most desktop browsers will scroll at the same time that the page is displayed (I'm sure that if you run this page in the desktop browser, it wonβt even let you scroll).
However, most mobile devices, looking to look fast and smooth, will draw the scroll separately from the page itself. The problem is that if JavaScript is stuck in a very long loop, the user will be able to scroll, but the page will not be displayed in front, eventually ending up in this empty white area where nothing is displayed.
Iβm sure that if you try to scale the page while the function is running, it will look very blurry, since rendering does not happen.
One way to fix this problem is to use timers or the requestAnimationFrame API.
requestAnimationFrame supported by almost all modern browsers. What he does is turn off the code until the next frame is rendered and is usually used to create smooth animations.
Using this API, I created the following code ...
var elements = document.querySelectorAll("P"); var i = 0; var j = elements.length; function iterate(){
Now the code above starts iterating the for loop for each frame of the animation. Assuming your device is running at 60 frames per second, this is no more than 60 iterations per second.
To combat this and make it even faster, we can replace requestAnimationFrame(iterate) with the alternative code window.setTimeout(iterate, 0) , which essentially tells the browser to iterate every millisecond that it can, although it can render the page. This method, however, can reduce the frame rate. (for a mobile browser that handles split scrolling like yours, however, frame rate should not be a problem).
Edit:
When I launched a rather simple but long cycle in my desktop browser using my method described above, I got JavaScript measured at 60 frames per second, with about 150 - 200 iterations per second. If you are on a mobile phone, your results are likely to be slower.
Alternative offer:
If you want to use a ready-made API for this kind of thing, there is one that looks pretty cool. The OP found one called Turboid, which makes it easier for you to control this type. http://turboid.net/artikel/real-loops-in-javascript.php