0

I'd like to know why setimout(fun(),time) is not working here:

Context: This shows a message and Hides it, i'd like to make it wait 2 seconds, but if i do as following it wont hide (normally i do it without the setimeout()

function mostrar_msj(msj){
  $('#notificaciones').text(msj);
  $('#notificaciones').animate({
          top:$(window).scrollTop()+"px"
      },
      {
          queue: false,
          duration: 350
  });  
  $("#notificaciones").slideDown(1000, setTimeout('cerrar()',2000));
}

function cerrar(){
  $("#notificaciones").fadeOut(2000);
}

I'm just confused, here :?

tvanfosson
  • 524,688
  • 99
  • 697
  • 795
Toni Michel Caubet
  • 19,333
  • 56
  • 202
  • 378
  • This isn't an answer to your question, but you are probably better off using jQuery's `delay` in this case. – Fareesh Vijayarangam Mar 23 '11 at 21:28
  • What are you trying to achieve? If you want the #notificaciones to fadeOut after they complete the slideDown, just use an anonymous function as the callback for slideDown. – Matijs Mar 23 '11 at 21:28

7 Answers7

8

As you are using jQuery, easier is to use delay():

$("#notificaciones").slideDown(1000).delay(2000).fadeOut(2000);

Animation functions are automatically queued.


But to answer your actual question:

  1. You are not setting the callback correctly. This

    $("#notificaciones").slideDown(1000, setTimeout('cerrar()',2000));
    

    will set the return value of setTimeout as callback for slideDown. A proper callback would be

    $("#notificaciones").slideDown(1000, function() {
        setTimeout('cerrar()',2000);
    });
    

    But this does not explain why the cerrar is not called as obviously setTimout is called. This brings us to the second point:

  2. If you pass a string to setTimeout, then it is evaluated in global scope. If you have this piece of code inside the ready handler, then cerrar is not in global scope and thus not found by JavaScript.

    For this reason, passing strings is discouraged. You should pass a function reference instead:

    setTimeout(cerrar, 2000);
    
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
2

It doesn't work because you didn't understand how to use callbacks. Here's the proper code:

function mostrar_msj(msj) {
    $('#notificaciones').text(msj);

    $('#notificaciones').animate({
        top: $(window).scrollTop() + "px"
    }, {
        queue: false,
        duration: 350
    });
    $("#notificaciones").slideDown(1000, function() {
        setTimeout(function() {
            $("#notificaciones").fadeOut(2000);
        }, 2000)
    });
}
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
1

Try this:

$("#notificaciones").slideDown(1000, function() { setTimeout('cerrar()',2000) });
Richard Dalton
  • 35,513
  • 6
  • 73
  • 91
0

You're referencing a function call when you should just reference the function:

...setTimeout(cerrar,2000)
RSG
  • 7,013
  • 6
  • 36
  • 51
0

Try passing cerrar as function pointer:

setTimeout(cerrar, 2000)

see: JQuery, setTimeout not working

Community
  • 1
  • 1
manji
  • 47,442
  • 5
  • 96
  • 103
0

I highly recommend not using setTimeout() if you are already using jQuery. Here is another way you could accomplish the same goal in a cleaner more jQuery-like fashion:

function mostrar_msj(msj){
        $('#notificaciones').text(msj);
        $('#notificaciones').animate({top:$(window).scrollTop()+"px" },{queue: false, duration: 350});  
        $("#notificaciones").slideDown(1000).delay(2000).fadeOut(2000);
}

The delay function takes the number of MS for the timeout as its argument, and will continue executing the queued/chained jQuery operations after the timeout.

adam
  • 3,888
  • 2
  • 20
  • 15
0

You should be able to do it this way, using the delay method

function mostrar_msj(msj){
    $('#notificaciones')
        .text(msj)
        .animate({top:$(window).scrollTop()+"px" },{queue: false, duration: 350})  
        .slideDown(1000)
        .delay(2000)
        .fadeOut(2000);
}
typeof
  • 5,812
  • 2
  • 15
  • 19