Chrome Extension

Run a function every 5 seconds interval using Javascript

If you want to run a javascript function every 5 seconds then you can use the setInterval() method of javascript. You can pass the time in milliseconds to this function.
var inverval_timer;

//Time in milliseconds [1 second = 1000 milliseconds ]    
inverval_timer = setInterval(function() { 
    console.log("5 seconds completed");
}, 5000);
    
//IF you want to stop above timer
function stop_timer() {
    clearInterval(inverval_timer); 
}
Best JSON Validator, JSON Tree Viewer, JSON Beautifier at same place. Check how cool is the tool

In the code snippet, we have created an interval function using setInterval() that will run every 5 seconds because we have passed 5000 as the time which it will wait to run the function again and again. It will print "5 seconds completed" in the console window every 5 seconds passed.

If you want to stop this function after some time or on a button click you can use clearInterval() method of javascript and pass the setInterval() object to it.

Was this helpful?
0 Comments