I have seen a lot of usages where the requestAnimationFrame
is being called and stopped, but I haven't seen anything regarding stopping it after x seconds. I have written this code:
http://codepen.io/anon/pen/PbvrVZ
And one snippet from my code, is like this:
function drawloop() {
if (focused) {
requestAnimationFrame(drawloop);
}
I just can't understand how to stop the animation after, let's say, 4 seconds. I am a newbie in JavaScript. Should I use timeOut or intervals?
Edit: So, what I did is, this:
function drawloop() {
var start = Date.now() // note this
if (focused & Date.now() - start < 4000) {
requestAnimationFrame(drawloop);
}
Yet this is still not working. What am I doing wrong here?