Is there a way to make some JS code be executed every 60 seconds? I'm thinking it might be possible with a while
loop, but is there a neater solution? JQuery welcome, as always.
repeat() is an inbuilt function in JavaScript which is used to build a new string containing a specified number of copies of the string on which this function has been called. Syntax: string. repeat(count);
Use setInterval() to run a piece of code every x milliseconds. You can wrap the code you want to run every second in a function called runFunction . Save this answer.
Within a browser, JavaScript doesn't do anything by itself. You run JavaScript from inside your HTML webpages. To call JavaScript code from within HTML, you need the <script> element.
Using setInterval:
setInterval(function() { // your code goes here... }, 60 * 1000); // 60 * 1000 milsec
The function returns an id you can clear your interval with clearInterval:
var timerID = setInterval(function() { // your code goes here... }, 60 * 1000); clearInterval(timerID); // The setInterval it cleared and doesn't run anymore.
A "sister" function is setTimeout/clearTimeout look them up.
If you want to run a function on page init and then 60 seconds after, 120 sec after, ...:
function fn60sec() { // runs every 60 sec and runs on init. } fn60sec(); setInterval(fn60sec, 60*1000);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With