Attention! This question is not about the best way to lay requestAnimFrame . If you are looking for this, skip to any other answer on this page.
You have been tricked by automatic semicolon insertion. Try the following:
window.requestAnimFrame = function(){ return ( window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function(/* function */ callback){ window.setTimeout(callback, 1000 / 60); } ); }();
javascript automatically puts a semicolon in your return . He does this because it is followed by a new line, and the next line is a valid expression. In fact, it translates into:
return; window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function(/* function */ callback){ window.setTimeout(callback, 1000 / 60); };
This code returns undefined and never executes the code behind the return statement. So window.requestAnimFrame is undefined . When you call it in animloop , javascript generates an error and stops execution. You can solve the problem by enclosing the expression in parentheses.
Can I recommend Chrome or firebug developer tools for checking javascript execution. With these tools you would see an error. You should debug it like this (I assume Chrome):
- Execute code (it gives unexpected results)
- Open the developer tools (right-click β Check item) You will see a red x in the status bar on the right (this means that there is an error in the execution).
- Open console tab
- You will see
Uncaught TypeError: property 'requestAnimFrame' of object [object DOMWindow] is not a function
- Enter the console:
window.requestAnimFrame and press enter, you will see that it is undefined . By now, you know that the problem is not really related to requestAnimationFrame and that you should focus on the first part of your code. - Now it is a matter of narrowing down the code to the point where it returns something. This is the hard part, and if you still canβt find it at this point, you can ask for help online for more help.
Also, watch this video for some good practices when writing javascript, it also mentions the introduction of an automatic automatic semicolon.
Jan 09 Apr 2018-11-11T00: 00Z
source share