Take the 2-minute tour ×

I was trying to do something along these lines:


which seems simple enough, but it is crippled by the "this" problem. I want to find a way to just pass the actual function as a parameter, without wrapping it in another function, e.g. I do not want to do this:


What I've tried:

setTimeout($('#element').hide.apply(document),3000);   /* jQuery docs say that document is the default context */
setTimeout($.proxy($('#element').hide,document),3000); /* I know this returns a function, which I don't want, but I have tried it */
setTimeout(($('#element').hide()),3000);               /* functional expression */

I'm looking for the way to remedy this problem, but I don't want to wrap it in another function. The less lines of code, the better. I know WHY this isn't working as expected, but HOW can I fix it without wrapping it in a closure?

share|improve this question
setTimeout will only take a function name, or anonymous function as a parameter, so I don't think this can be done as you want. What is your reason for not wanting to use an anonymous function? –  Chris Wheeler Nov 26 '13 at 15:26
In the article you gave us, they give a possible solution. Why don't you try it ? –  Romain Braun Nov 26 '13 at 15:27
@epascarello Those are what I've tried. Seven different attempts at a single line of code. –  Alex W Nov 26 '13 at 15:30
Just wrap it in an anonymous function (or define a wrapper function just before the setTimeout, and pass that function to setTimeout). So much cleaner than any of these other options. (Although if I had to pick one of them, it would probably be $.proxy. But... Just. Use. The. Anonymous. Function. (or explain why not...) :) –  Shai Nov 26 '13 at 15:33
add comment

1 Answer

up vote 3 down vote accepted

You can do this way by binding the context of the method with the element itself so that in jquery hide method this will point to jquery object and not global context. You can create bound functions using:


Cross Browser Alternative for this:



var $elem = $('#element');


setTimeout($.proxy($elem.hide, $elem),3000);


setTimeout($.fn.hide.bind($elem),3000); //setTimeout($.proxy($.fn.hide, $elem),3000);


share|improve this answer
nice. if you are re-binding anyway - you could use $.fn.hide rather than $elem.hide –  benjaminbenben Nov 26 '13 at 15:32
setTimeout($.proxy($.fn.hide, $('#element'))) is shorter. –  haim770 Nov 26 '13 at 15:33
@benjaminbenben Oh yeah True... updated... Thanks. –  PSL Nov 26 '13 at 15:33
I really like your fiddle because it's a one-liner without a variable, but I'm confused about why .bind() is allowing you to pass a jQuery object? The documentation says the first parameter should be a string. –  Alex W Nov 26 '13 at 15:48
@AlexW I am not using jquery.bind it is Function.bind, jquery bind is for attaching events. Equivalent for function.bind is jquery.proxy. It bind a context to a function reference, so that when invoked it will have this context as that of the context set. It is also called Bound Functions –  PSL Nov 26 '13 at 15:49
add comment

Your Answer


