1

In code below, setInterval does not work as you would expect i.e to call function greet after every 3s. It calls greet the first time and that too before 3s.

name = "Superman";

function greet(name) {
  alert("Hello " + name)
}

setInterval(greet(name), 3000);

Can any body tell me what am I doing wrong.

Thanks bt

b Tech
  • 412
  • 4
  • 14
  • @Quentin shouldn't be duplicate of **[Pass parameters in setInterval function](http://stackoverflow.com/questions/457826/pass-parameters-in-setinterval-function)** ? – Zakaria Acharki Sep 26 '16 at 08:35
  • @ZakariaAcharki — That's also a valid duplicate, although I think the one I found was better. – Quentin Sep 26 '16 at 08:37

4 Answers4

1

The first argument of setInterval needs to be a function.

You are calling greet immediately and passing its return value (undefined since there is no return statement) to setInterval.

If you want to call greet with arguments on an interval, create a new function which does that and pass that function.

var name = "Superman"; // Added `var` here. Implicit globals are problematic and banned in strict mode.

function greet(name) {
  alert("Hello " + name)
}

function greetName() {
  greet(name);
}

setInterval(greetName, 3000);
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Your first line can be requoted as >The first argument of setInterval needs to be a function **definition**. Is that correct? – b Tech Sep 26 '16 at 08:43
  • @bTech — Not really. A function definition is the JavaScript source code used to create a function. It's the output of that code which needs to be passed. – Quentin Sep 26 '16 at 08:45
  • I don't fully understand. In the code above, if you pass `greetName` (which is just a reference) you are actually passing `function greetName() { greet(name); }` the definition to `setInterval`. Also as per [w3schools.com](http://www.w3schools.com/jsref/met_win_setinterval.asp), > `The setInterval() method calls a function or evaluates an expression at specified intervals (in milliseconds).` So actually `setInterval` does the calling (or execution) for which it needs the reference to the callback's definition. Pls correct me if I am wrong (perhaps with an example) as I need to understand it. – b Tech Sep 26 '16 at 11:12
1

If you use the variable in a global way, like your example. You do not need to have a parameter in the function. Then you could just do this:

name = "Superman";

function greet() {
  alert("Hello " + name)
}

setInterval(greet, 3000);

But, if you still want to have a parameter, you could do this:

name = "Superman";

function greet(name) {
  alert("Hello " + name)
}

setInterval(function () {
  greet(name);
}, 3000);
H0wie12
  • 33
  • 1
  • 4
0

try this:

name = "Superman";

function greet(name) {
  alert("Hello " + name)
}

setInterval(function() { return greet(name) }, 3000);
Bartłomiej Gładys
  • 4,525
  • 1
  • 14
  • 24
-1

You should add anonymous function function(){} to the call of function inside setInterval() since you pass parameters to it :

name = "Superman";

function greet(name) {
  alert("Hello " + name)
}

setInterval(function() { greet(name) }, 3000);

Or just pass parameters as arguments at the end, like :

setInterval(greet, 3000, name);

Hope this helps.

name = "Superman";

function greet(name) {
  console.log("Hello " + name)
}

//setInterval(function() { greet(name) }, 1000);
//Or
setInterval(greet, 1000, name);
Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101