I'm using the requestAnimFrame
method in JavaScript to make my canvas update within my program's main loop:
window.requestAnimFrame = (function(callback) {
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame ||
function(callback) {
window.setTimeout(callback, 1000 / 60);
};
})();
function animate() {
requestAnimationFrame( animate );
//var runcount = 100;
//for (var i=0;i<=100;i++) {
draw();
// if (runcount === i)
// alert("Completed program loop");
// }
}
What was happening was that my program that only updated the canvas after my main loop had run for 100 iterations and then stopped. After I add the method above into my main loop, suddenly I get canvas updates on every loop of my program.
The fact that the canvas didn't get updated until my program had finished made me think that the canvas update was running in a different thread that didn't get priority. (But we know that JavaScript is single-threaded).
My question is - Can we consider an requestAnimFrame a 'yield' on the application loop to allow the event loop to process? Can I still assume that JavaScript is single threaded?