Gooey
Gooey

Reputation: 4778

setTimeout function always triggers

I'm using the following code to hide a div (named info) when you click outside of it:

$(document).click(function(e) {
if (e.target.id != 'info' && !$('#info').find(e.target).length) {
        $("#info").fadeOut(300);
setTimeout(function() {
        $("#info").html("Select an Item");
},300);
}; //if statement
}); //click function

What i'm trying to achieve is after the fadeOut is done, to place the text "Select an Item" in the div. However this SetTimeout is always executed; the div shows itself through another function, but it seems that this setTimeout function also triggers, immediately.

Why does this happen and how do you fix this?

Upvotes: 0

Views: 244

Answers (2)

travis
travis

Reputation: 8615

I would avoid the setTimeout if you can, instead do this:

$("#info").fadeOut(300, function() {
  // Animation complete.
  $("#info").html("Select an Item");
});

Upvotes: 2

anAgent
anAgent

Reputation: 2780

I better solution is to use the delay function:

$("#info").delay(1000).html("select an item").fadeOut(300);

References:

Upvotes: 0

Related Questions