Ega Rana
Ega Rana

Reputation: 67

What is the difference between this function call?

setInterval("RunSlide()", 5000);

First, call a function with brackets and quotes.

setInterval("RunSlide", 5000);

Second, call a function without brackets but still using a quotes.

setInterval(Runslide, 5000);

Third, call a function without brackets and quotes.

RunSlide();

Fourth, call a function with brackets.

RunSlide;

Fifth, call a function without brackets.

Upvotes: 2

Views: 144

Answers (1)

Filip Roséen
Filip Roséen

Reputation: 63797

Even though the result might be the same there are differences in using the different argument types.


  • setInterval ("argument as string", delay)

    This will cause the same behavior as passing the string to eval and is therefore not recommended (for the same exact reasons, more on that subject can be found on the web).

example snippet:

 setInterval ("console.log ('hello world');", 100);

  • setInterval (func_reference, delay, param1, param2, ...)

    This will cause the function pointed to by func to be called, this is the recommended option. If you call the function this way you will also be able to pass parameters to the function to be executed.

example snippet(s):

function say_it (word1, word2) {
  console.log (word1 + " " + word2);
}

setInterval (say_it, 100, "hello", "world");

setInterval (function (word1, word2) {
   console.log (word1 + " " + word2);
}, 100, "hello", "world);

Note(s)

Calling setInterval with the first argument as a string containing only the name of a function is utterly pointless, the function will not be executed. It's the same as writing the below somewhere in your code.

alert;

The differences between RunSlide(); and RunSlide; should be quite obvious after the above example, one will call the function referenced by the name RunSlide, the other will not (do anything at all).

Upvotes: 0

Related Questions