I want to cancel an animation after a timeout. Here's my code
function animate(){
id = requestAnimationFrame(animate);
console.log(id);
c.clearRect(0,0, window.innerWidth, window.innerHeight);
for(let i = 0; i < circleArray.length; i++){
circleArray[i].scatter();
}
setTimeout(function(){
id = requestAnimationFrame(animate);
cancelAnimationFrame(id);
update();
},5000)
}
function update(){
requestAnimationFrame(update);
c.clearRect(0,0, window.innerWidth, window.innerHeight);
for(let i = 0; i < circleArray.length; i++){
circleArray[i].update();
}
}
Here, I want to stop the recursion of the animate function and start the update function. But the animate doesnt stop and update function runs concurrently after the given time.