jeudi 15 février 2018

Using requestAnimationFrame() as setInterval() Javascript

I am currently in a situation where I am using setInterval() in order to create buttons which flicker at particular frequencies on screen. Like so: (element refers to the button)

setInterval(function() { 
   if($(element).css("background-color") === darkColor){ //if colour is dark
       $(element).css("background-color", lightColor); //set to light

   }else if($(element).css("background-color") === lightColor){ //if colour is light
       $(element).css("background-color", darkColor); //set to dark
   }
}, frequency); //frequency value is in milliseconds

I lately read on the Internet that requestAnimationFrame() function is generally better for displaying timed on-screen animations. Do you know how requestAnimationFrame() can be used in such a situation or if it is even suitable to use it in such a situation?

Note: It is mandatory that Javascript is used to perform the flicker effect (no CSS)

Thank you




Aucun commentaire:

Enregistrer un commentaire