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.