Run a function every 30 seconds javascript

2020-07-18 06:37发布

问题:

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

回答1:

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)


回答2:

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.



回答3:

function blah(){}

blah();
setInterval(blah,30000);