Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run JavaScript function at regular time interval

Tags:

javascript

I am currently building a website to host software. What I want is to add to the project pages is a slideshow of screenshots cycling, changing images about every 5 seconds. Is there any way to a script triggered at a time interval using just JavaScript? ..or will I have to resort to alternative methods for achieving my desired functionality. Thanks in advance for any help!

like image 319
DeathByTensors Avatar asked Aug 06 '13 02:08

DeathByTensors


People also ask

How do you call a function at a regular interval?

The setInterval() method calls a function at specified intervals (in milliseconds). The setInterval() method continues calling the function until clearInterval() is called, or the window is closed. 1 second = 1000 milliseconds.

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.

What is interval in JavaScript?

In JavaScript, a block of code can be executed in specified time intervals. These time intervals are called timing events. There are two methods for executing code at specific intervals. They are: setInterval()

How do you call a function after a period of time?

You can use JavaScript Timing Events to call function after certain interval of time: This shows the alert box every 3 seconds: setInterval(function(){alert("Hello")},3000); You can use two method of time event in javascript.


4 Answers

setInterval:

function doSomething() {
    alert('This pops up every 5 seconds and is annoying!');
}

setInterval(doSomething, 5000); // Time in milliseconds

Pass it the function you want called repeatedly every n milliseconds. (setTimeout, by the way, will call a function with a timeout.)

If you ever want to stop the timer, hold onto setInterval’s return value and pass it to clearInterval.

like image 52
Ry- Avatar answered Oct 09 '22 22:10

Ry-


You want the setInterval function.

setInterval(function() {
  // This will be executed every 5 seconds
}, 5000); // 5000 milliseconds

Basic reference: http://www.w3schools.com/jsref/met_win_setinterval.asp (please ignore the reference to the "lang" parameter)

More indepth reference: https://developer.mozilla.org/en-US/docs/Web/API/window.setInterval

like image 24
Andrew Magee Avatar answered Oct 09 '22 21:10

Andrew Magee


You can use window.setInterval

Sample usage:

window.setInterval(function () {
    console.log("foo");
}, 3000);
like image 4
rexcfnghk Avatar answered Oct 09 '22 22:10

rexcfnghk


It Changes the date time in a div and time changes frequently after 1 sec.

    setInterval(function(){
      var date=new Date();
      $('.class').html(date);
    },1000);
like image 1
Pradeep Kumar Das Avatar answered Oct 09 '22 21:10

Pradeep Kumar Das