Reputation: 1329
On the MDN page for window.setTimeout
I find this example where a named function is passed to window.setTimeout:
var timeoutID;
function delayedAlert() {
timeoutID = window.setTimeout(slowAlert, 2000);
}
function slowAlert() {
alert("That was really slow!");
}
function clearAlert() {
window.clearTimeout(timeoutID);
}
In code I am maintaining, I have encountered the equivalent of this example where an anonymous function is declared as it is passed to window.setTimeout
:
var timeoutID;
function delayedAlert() {
timeoutID = window.setTimeout(function(){
alert("That was really slow!");
}, 2000);
}
function clearAlert() {
window.clearTimeout(timeoutID);
}
Is there an important difference between these two ways of delaying the alert? I trust MDN more than the code I'm working with, so I want to understand why MDN phrased their example using a separate function declaration.
edit: Thank you @TravisJ @jfriend00 @PlatinumAzure for the clear and helpful answers.
Upvotes: 10
Views: 5102
Reputation: 707238
There is no functional difference as you have them between using an inline anonymous function versus a separately declared named function. Both will run identically.
Advantages of the anonymous function:
Advantages of the named function:
FYI, an inline declared function can also have a name if desired so the two forms can be mixed too.
My personal choice is to use an inline anonymous function unless there is explicitly some reason not too and I choose this for the advantages of the anonymous function named above.
Upvotes: 3
Reputation: 82267
There is not really much of a difference if it isn't being done much. The anonymous function is going to use some insignificantly more amount of memory than the separate function.
The reason is that the separate function declaration is going to be used essentially as a pointer so it may be re-used. However, the anonymous function is going to need to be constructed every time. It is a very small difference.
The basic difference is going to be scoping and parameters. You cannot pass parameters to a function pointer. Does the function inside of the timeout need to share scope with its parent? If so, then an anonymous function may be more worthwhile than a declared function in another scope. A common one is to pass in this
.
var that = this;
window.setTimeout(function(){
//keep in mind this anonymous function's `this` will be window
showAlert(that.message);
},2000;
In order to pass a message, for example if your alert was function showAlert(msg)
then you would need to use an anonymous function window.setTimeout(function(){showAlert("hello");}, 2000);
.
What you really want to avoid using is a string there. Such as
window.setTimeout("slowAlert()", 2000);
This is considered bad practice because a Function will be constructed based off of the string similar to using eval
.
Upvotes: 8
Reputation: 46183
Both approaches will work and are functionally equivalent.
My suggestion would be to use a named function if that function is long and would cause the code to be harder to read if rendered inline, and to use an inline anonymous function for quick one-liners. But honestly, it's up to you.
Upvotes: 2