0

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>

10
  • You have to use jquery setTimeout function. You want the timeout approach? Commented Feb 9, 2017 at 5:58
  • @SorangwalaAbbasali Why would you need JQuery for that? Commented Feb 9, 2017 at 5:58
  • 3
    <div myFunc(5)> this doesn't make any sense Commented Feb 9, 2017 at 5:59
  • if you had called... then it would generate infinate tymes of interval instances recursively Commented Feb 9, 2017 at 6:00
  • and why from div you want to call means how on click, on hover or something else ? Commented Feb 9, 2017 at 6:01

2 Answers 2

1

JavaScript Window setInterval() Method see

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

JS Bin

Sign up to request clarification or add additional context in comments.

Comments

0

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);
      }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.