Why jQuery setTimeout doesn’t work

Note to self:

It doesn’t work because this is a different context at that point (window), you have a few optons though, store a reference to what you want to deal with, like this:

.ajaxStop(function() { var $this = $(this); setTimeout(function(){ $this.css('visibility','hidden'); }, 100); });

Or use $.proxy() for to set the context in that anonymous function, like this:

.ajaxStop(function() { setTimeout($.proxy(function(){ $(this).css('visibility','hidden'); }, this), 100); });

In the first solution we store a reference to what we want to deal with, in the second we’re actually setting that this is when that function runs…otherwise it’ll be window.

From stackoverflow: