AnonymousUser
AnonymousUser

Reputation: 786

How to make the animation run every x seconds

I want an animator that if I have like this

index = 0;
function animate() {
    index++;
    requestAnimationFrame(animate);
}

But how do I make it so index++ is every x second? so if x is 5 for example index += 1 every 5 second/ the animation makes a loop every 5 second.

Upvotes: 1

Views: 823

Answers (3)

Nguyễn Văn Phong
Nguyễn Văn Phong

Reputation: 14208

You can use setInterval() like this

index = 0;
function animate() {
    index++;
    console.log(index)
    //requestAnimationFrame(animate);
}

setInterval(function(){  
  animate();
}, 5000);

Upvotes: 1

MatthewCook0485
MatthewCook0485

Reputation: 11

Something like this should help you

let start = Date.now();

function foo() {
    if(Date.now() - start > 5000){
    console.log('hit')
   start = Date.now()
}
 requestAnimationFrame(foo);
}

foo();

Upvotes: 1

DudhaneShrey86
DudhaneShrey86

Reputation: 11

You can look up for the setInterval() function in js.
Here's a link to begin with https://www.w3schools.com/jsref/met_win_setinterval.asp

Upvotes: 0

Related Questions