What's the easiest way to call a function every 5 seconds in jQuery? [duplicate] What's the easiest way to call a function every 5 seconds in jQuery? [duplicate] javascript javascript

What's the easiest way to call a function every 5 seconds in jQuery? [duplicate]


You don't need jquery for this, in plain javascript, the following will work!

var intervalId = window.setInterval(function(){  /// call your function here}, 5000);

To stop the loop you can use

clearInterval(intervalId) 


you could register an interval on the page using setInterval, ie:

setInterval(function(){     //code goes here that will be run every 5 seconds.    }, 5000);


A good example where to subscribe a setInterval(), and use a clearInterval() to stop the forever loop:

function myTimer() {    console.log(' each 1 second...');}var myVar = setInterval(myTimer, 1000);

call this line to stop the loop:

 clearInterval(myVar);