Which of these setTimeout format is better?

javascript, jquery, syntax

Solution

The first one for sure, it uses an anonymous function to capture the function to execute after the timeout.

Second one uses `eval()` to evaluate your string, which would probably be slower than the first option (nevermind the arguments for why using `eval()` is bad).

Third one shows the element over 5 seconds and then hides as soon as it completes, therefore is different from the first.

UPDATE:

nickf's update prompted me to look through the source and number 3 will execute immediately if the element is already visible. Here are the relevant lines of source code

if ( prop[p] == "hide" && hidden || prop[p] == "show" && !hidden ) 
    return opt.complete.call(this);

Problem

Which of these constructs is better and why ``` setTimeout(function() { $('#secret').hide(); }, 5000); setTimeout( "$('#secret').hide();", 5000); $('#secret').show(5000, function(){ this.hide(xxx)} ); ```

Original source