I don’t know why but this code is not working ? Why would it not ? I guess it is because scope problem I am having here :
function washAway(obj) {
alert($(obj)); // says HTML Object which is fine
setTimeout(function() {
alert($(obj)); // says undefined
$(obj).fadeOut("slow", function() {
$(this).remove();
});
}, 2000);
};
At the point where the function in the timeout executes, it has no way to know what
objis – it was a parameter passed into the method where the timeout was set up, but the function inside has no reference to it.An alternative approach is to write a jQuery plugin to wait before it acts like this:
Then you can use: