Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call a Javascript function every 5 seconds continuously [duplicate]

Possible Duplicate:
Calling a function every 60 seconds

I want to Call a Javascript function every 5 seconds continuously. I have seen the setTimeOut event. Will it be working fine if I want it continuously?

like image 287
deepmoteria Avatar asked Aug 25 '11 09:08

deepmoteria


People also ask

How do you run a function every 5 seconds in JavaScript?

To call a JavaScript function every 5 seconds continuously, we call setInterval with the function that we want to run and the interval between runs. const interval = setInterval(() => { // ... }, 5000); clearInterval(interval); to call setInterval with the callback we want to run and 5000 millisecond period.

How do you call a function after 5 seconds?

How do I delay a function call for 5 seconds? A setTimeout is a native JavaScript function, which calls a function or executes a code snippet after a specified delay (in milliseconds). A setTimeout accepts a reference to a function as the first argument. Alert messages will pop up after 5 seconds automatically.

How do you call a function after every second?

Answer: Use the JavaScript setInterval() method You can use the JavaScript setInterval() method to execute a function repeatedly after a certain time period. The setInterval() method requires two parameters first one is typically a function or an expression and the other is time delay in milliseconds.


1 Answers

You can use setInterval(), the arguments are the same.

const interval = setInterval(function() {    // method to be executed;  }, 5000);  clearInterval(interval); // thanks @Luca D'Amico 
like image 191
Anantha Sharma Avatar answered Sep 22 '22 12:09

Anantha Sharma