1

I have an animation with requesetAnimationFrame And when I replay it, it gets faster and faster until it gets invisible.

function tirer(){

    var missile=document.getElementById("missile");
    var currt= missile.currentStyle ||window.getComputedStyle(missile);
    if ( parseInt(currt.bottom)<700)
    missile.style.bottom=parseInt(missile.style.bottom)+20+"px";
    else
    alert ( "in top");


}
function animation (){
    tirer();
    requestAnimationFrame(animation);

}

How can I get it to be a stable, constant speed?

Tony Hinkle
  • 4,706
  • 7
  • 23
  • 35
unfoudev
  • 87
  • 1
  • 7

1 Answers1

2

You probably call animation() multiple times (e.g. whenever a button is clicked).

Each call to animation() starts a new repeating requestAnimationFrame() -> animation() -> requestAnimationFrame() -> animation() -> ... sequence.

E. g. if you call animation() two times, your have two such sequences running and your framerate will double.

You need to cancel the currently ongoing request for an animation frame before sending a new request. See How to stop a requestAnimationFrame recursion/loop?

Community
  • 1
  • 1
le_m
  • 19,302
  • 9
  • 64
  • 74