12

I am trying to run a function every 30 seconds but setInterval waits 30 seconds then runs it repeatedly. So if there are any other methods of going about this. (Without 3rd party plugins)

Any help would be appreciated

user2580555
  • 329
  • 2
  • 4
  • 13

3 Answers3

22

Based on the answer from "Schechter" but fixed to run on the first page load and then runs every 30 secs.

function myFunction(){
    console.log('myFunction Called')
}

myFunction();

setInterval(function(){
    myFunction()
}, 30000)
Sebastian
  • 1,321
  • 9
  • 21
Gary Carlyle Cook
  • 728
  • 11
  • 26
5
function foo(){
    console.log('function is being called')
}

setInterval(function(){
    foo()}, 30000)

The second argument in setInterval is the time delay in milliseconds, so use 30000 for 30 seconds, not 30.

Schechter
  • 224
  • 2
  • 5
4
function blah(){}

blah();
setInterval(blah,30000);
Matt
  • 3,638
  • 2
  • 26
  • 33