I have some questions concerning JavaScript loops.
Questions :
- Why does a JavaScript loop freeze the browser
- Why is the drawing slow even do it's running at 1 draw every 1ms and it's drawing the simplest thing!
- What's the solution? flash is dying, what do we do now?
Here is the canvas code to try for yourself :
<!doctype html>
<html>
<head>
</head>
<body>
<canvas id="c" width="400" height="400"></canvas>
<script type="text/javascript">
var c = document.getElementById( 'c' );
ctx = c.getContext( '2d' );
var x = 100;
ctx.fillStyle= '#f00';
function loop()
{
ctx.fillRect( x, 100, 20, 20 );
++x;
}
setInterval( loop, 1 );
</script>
</body>
</html>
setInterval()replace it withwindow.requestAnimationFrame()