0

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?

4

2 回答 2

2

setTimeout如果可以的话,我会避免这样做,而是这样做:

$("#info").fadeOut(300, function() {
  // Animation complete.
  $("#info").html("Select an Item");
});
于 2012-08-09T20:37:07.667 回答
0

我更好的解决方案是使用延迟功能:

$("#info").delay(1000).html("选择一个项目").fadeOut(300);

参考:

于 2012-08-09T20:37:03.843 回答