慢速计算机上的jQuery

时间:2009-04-06 17:59:51

标签: jquery

我正在使用jQuery创建一个在线游戏。由于对较慢的计算机有很多淡入淡出效果,我做了一个设置jQuery.fx.off = true;的选项,导致此代码出现问题:

$('<div>'+msg+'</div>').fadeOut(5000,function({$(this).remove()}).appendTo('#msg');

此代码在#msg div中显示消息,然后在5秒后消息消失。可以有许多消息,可以随时添加。如果因为fadeOut立即完成,jQuery.fx.off = true;消息将不会显示:(当fx.off为true时,我需要的是$('#id').delay(5000).remove()

我在jQuery的帮助下寻找它,但没有找到。

1 个答案:

答案 0 :(得分:2)

这可能是或者可能不是您的问题的可接受的解决方案,但您可以创建一个名为maybeFadeOut的新JQuery函数。

jQuery.fn.maybeFadeOut = function (time, fun) {
  if (slowComputer) {
    setTimeout(function () {
      $(this).remove();
      if (fun) fun(); // Optionally run the callback function.
    }, time);
    return $(this);
  } else {
    return $(this).fadeOut(5000, function () { $(this).remove; });
  } 
};
相关问题