0

I'm trying to call a function every x-seconds,

setInterval( alert("Hello"),2000);

But the alert function appear just for the first call,

Yacino
  • 645
  • 2
  • 10
  • 32

1 Answers1

0

You may try this:

setInterval(function() {
    alert("Hello");
} ,2000);

It's not working like you thought. The alert fires/runs directly without interval because you've directly called it. To be more clerer, in your code:

setInterval(alert("Hello"),2000);

You are calling the alert("Hello") but you should pass a function (or bind) which you want to run on an interval, which is:

setInterval(function(){
    // ...
}, 2000);

So the passed anonymous function will be called using the given interval (bind is another way but I've used a function/closure for simplicity to clarify you).

The Alpha
  • 143,660
  • 29
  • 287
  • 307