tags:

views:

142

answers:

1

I'm creating an online game using jQuery. Due to lot of fade effect for slower computers I did an option that sets jQuery.fx.off = true; That caused a problem with this code:

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

This code display messages in the #msg div, then after 5 seconds message is gone. There can be many messages and they can be added in any time. When jQuery.fx.off = true; message won't appear because fadeOut is done instantly :( What I need is something like $('#id').delay(5000).remove() when fx.off is true.

I was looking for it in jQuery's help, but nothing found.

+2  A: 

This may or may not be an acceptable solution to your problem, but you could create a new JQuery function called maybeFadeOut or something.

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; });
  } 
};
Deniz Dogan
It doesn't work, since $(this) in setTimeout function is probably empty.
Thinker
I never tested the code myself, but I'm pretty sure that if $(this) is defined outside of the setTimeout, it will also be defined in the setTimeout. This is actually a feature in many programming languages and is very useful in JavaScript in particular.
Deniz Dogan