Instead of
// #1
setTimeout(visualize(file), 2000);
you want
// #2
setTimeout(function() {
    visualize(file);
}, 2000);
or on modern browsers, you can provide arguments to pass to the function after the delay:
// #3
setTimeout(visualize, 2000, file);
Those three explained:
- (As SLaks mentions) This calls visualizeimmediately, and then passes its return value intosetTimeout(and sincevisualizecalls itself, it keeps calling itself recursively and you end up with a stack overflow error).
- This passes a function reference into setTimeoutthat, when called, will callvisualizeand pass it thefileargument (with its value as it is then). The function we're passing intosetTimeoutwill have access to thefileargument, even though your code has run and returned, because that function is a closure over the context in which it was created, which includesfile. More: Closures are not complicated Note that thefilevariable's value is read as of when the timer fires, not when you set it up.
- This passes the visualizefunction reference intosetTimeout(note we don't have()or(file)after it) and also passesfileintosetTimeout, using its value as of when you set up the call. Later, in modern environments,setTimeoutwill pass that on to the function when calling it later.
There's an important difference between #2 and #3: With #2, if file is changed between when setTimeout is called and the timer expires, visualize will see file's new value. With #3, though, it won't. Both have their uses. Here's an example of that difference:
let file = 1;
// #2, using "file" when the timer fires, not when you set it up
setTimeout(function() { visualize(file); }, 2000); // Shows 2
// #3, using "file" right away when setting up the timer
setTimeout(visualize, 2000, file); // Shows 1
file = 2;
function visualize(value) {
    console.log(value);
}
 
 
If you needed #3's behavior of immediately reading file (rather than waiting until the timer fires) in an environment that didn't support extra arguments to setTimeout, you could do this:
// #4 (for environments that don't support #3)
setTimeout(visualize.bind(null, file), 2000);