CoreyCooper
CoreyCooper

Reputation: 3

How to call a JavaScript function after n seconds

i want to call a javascript function from a div every seconds. and i also pass arguments with this function

function myFunc(a) {
    alert(a);
    setInterval("myFunc(a)", 1000); 
}
myFunc(a);
<div myFunc(5)></div>

Upvotes: 0

Views: 482

Answers (2)

Kayis Rahman
Kayis Rahman

Reputation: 352

JavaScript Window setInterval() Method see

<button onclick="myFunction('param')">Try it</button>
<script>
function myFunction(a) {
   setInterval(function(){ alert(a); }, 1000);
}
</script>

JS Bin

Upvotes: 1

Gauri Bhosle
Gauri Bhosle

Reputation: 5473

Try below code where page will be auto refreshed after 1min and that time on page load you are calling your function

 var refresh_mili_sec=60000;  //60000=1min
    $(document).ready(function(){
    var a=5;
    myFunc(a);

     setTimeout(function()
        {
             window.location.reload(1);
         }, refresh_mili_sec);
    });

    function myFunc(a) {
        alert(a);
      }

Upvotes: 0

Related Questions