-3

I need to refresh a div on 3 sec so I tried a setInterval method as follows using javaScript:

<button onclick="myFunction()">Try it</button>

<script>

function hello(){
alert("vannallo");
}

function myFunction() {
    setInterval(hello(), 3000);
}

</script>

This is not working, But when I tried like thje following it works:

function myFunction() {
        setInterval(function hello(){
    alert("vannallo");
    }, 3000);
}

I need to work setInterval continuosly on my first buttonclick , How can I acheive it? Please help guys!!

user1365067
  • 95
  • 3
  • 13
  • 1
    Search a little bit harder on SO for the answer to this question, which has come up many times. Hint: specifying a function is different from calling it. –  Dec 17 '14 at 13:53

1 Answers1

4

You aren't passing your function to setInterval but what it returns (that is undefined).

Change

setInterval(hello(), 3000);

to

setInterval(hello, 3000);
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758