在jquery中,单击设置一个淡出或淡出延迟

在jquery中,单击设置一个淡出或淡出延迟,jquery,notifications,Jquery,Notifications,我正在尝试更正弹出通知的脚本。我不希望弹出窗口在X秒后淡出,也不希望在用户单击消息时淡出。我可以让这两种效果单独发挥作用,但当我尝试将它们结合起来时,淡出效果会起作用。这是我目前的代码: function notify(data, type) { switch(type) { case "success": $('#notify').html(data) .removeAttr("style") .addClass('notifySuccess') .click(functi

我正在尝试更正弹出通知的脚本。我不希望弹出窗口在X秒后淡出,也不希望在用户单击消息时淡出。我可以让这两种效果单独发挥作用,但当我尝试将它们结合起来时,淡出效果会起作用。这是我目前的代码:

function notify(data, type) {
 switch(type) {
 case "success":
  $('#notify').html(data)
  .removeAttr("style")
  .addClass('notifySuccess')
  .click(function() {
$("#notify").fadeOut();
  })
  .delay(5000).fadeOut();
break;
 case "error":
  $('#notify').html(data)
  .removeAttr("style")
  .addClass('notifyFailure')
  .click(function() {
$("#notify").fadeOut();
  })
  .delay(5000).fadeOut();
break;
}
}
您需要在那里输入一个命令来清除队列中的延迟,如下所示:

function notify(data, type) {
 switch(type) {
 case "success":
  $('#notify').html(data)
  .removeAttr("style")
  .addClass('notifySuccess')
  .click(function() {
    $("#notify").stop(true, true).fadeOut();
  })
  .delay(5000).fadeOut();
break;
 case "error":
  $('#notify').html(data)
  .removeAttr("style")
  .addClass('notifyFailure')
  .click(function() {
    $("#notify").stop(true, true).fadeOut();
  })
  .delay(5000).fadeOut();
break;
}
}
function notify(data, type) {
 $('#notify').html(data)
   .removeAttr("style")
   .addClass(type == 'success' ? 'notifySuccess' : 'notifyFailure')
   .delay(5000).fadeOut()
   .click(function() {
     $(this).stop(true, true).fadeOut();
   });
 }
此外,如果类型只有
success
error
,则可以将其大大缩短,如下所示:

function notify(data, type) {
 switch(type) {
 case "success":
  $('#notify').html(data)
  .removeAttr("style")
  .addClass('notifySuccess')
  .click(function() {
    $("#notify").stop(true, true).fadeOut();
  })
  .delay(5000).fadeOut();
break;
 case "error":
  $('#notify').html(data)
  .removeAttr("style")
  .addClass('notifyFailure')
  .click(function() {
    $("#notify").stop(true, true).fadeOut();
  })
  .delay(5000).fadeOut();
break;
}
}
function notify(data, type) {
 $('#notify').html(data)
   .removeAttr("style")
   .addClass(type == 'success' ? 'notifySuccess' : 'notifyFailure')
   .delay(5000).fadeOut()
   .click(function() {
     $(this).stop(true, true).fadeOut();
   });
 }
试着放


非常感谢!!!这正是我想要的。我也会按照你的建议减少代码,再次谢谢!