views:

116

answers:

2

Hello.

function checkSession(){
    $.ajax({url: "session.php", success: function(data){
         if( data == 1){
            var postFilen = 'msg.php';
            $.post(postFilen, function(data){
            $(".msg").html(data).find(".message2").fadeIn("slow")
         }else{ 
             $('.msg').hide();
         }
    }});
// setInterval('checkSession()',1000);

Now i want to place to fadeout .msg after 5 seconds it has been shown. How do i do this.. i have tried:

function checkSession(){
    $.ajax({url: "session.php", success: function(data){
         if( data == 1){
            var postFilen = 'msg.php';
            $.post(postFilen, function(data){
            $(".msg").html(data).find(".message2").fadeIn("slow")
                    setTimeout(function() {
    $('.msg').fadeOut('slow');
        }, 5000);
            });  
         }else{ 
             $('.msg').hide();
         }
    }});
// setInterval('checkSession()',1000);
}

But then the message wont appear after 1st time..

+1  A: 

Try to work with the delay method: http://api.jquery.com/delay/

 $('#foo').slideUp(300).delay(800).fadeIn(400);

I your case, something like:

 $(".msg")
   .html(data)
   .find(".message2")
   .fadeIn("slow")
   .parent('.msg')
   .delay(5000)
   .fadeOut('slow')

[edit: fixed example]

[edit2: new example]


This one seems to work fine here:

<div class="msg"></div>
<p>
    Some text <br /> <a id="bloup" href="">show message</a>
</p>

and then

   $(function() {
      $("#bloup").click(function(e) {
          e.preventDefault();

          var data = "<span class='message2'>Hello world</span>";

          $(".msg")
              .show()
              .html(data)
              .find('.message2')
              .fadeIn('slow')
              .parent('.msg')
              .delay(2000).fadeOut('slow');
      }) 
   });
Romuald Brunet
Doesnt work.. 1st time it fadeout the same time it was going to show and then second and rest of the time it didnt appear
Karem
Correct, the parent and delay where inverted. Fixed my example.
Romuald Brunet
+1 A good use of `delay`.
Andy E
Now it do fadeout like it should first time, but second and rest of the time it wont appear again.. Just like my problem was when i tried myself
Karem
Maybe because you use `$('.msg').hide()` but do not use `.show()` afterwards ? Since .message2 is contained in .msg, if .msg is hidden, .message2 won't be displayed :)
Romuald Brunet
ok so i removed the whole else{ $('.msg').hide() } , and STILL not displaying after first time
Karem
Correct :/ Because `fadeOut` will hide the .msg blockTry adding `.show()` after the `fadeOut()`
Romuald Brunet
That worked!!! Great1
Karem
A: 

First try to write cleaner code for us to understand your problem better, your $.post's callback function missing }); these chars at its end on first snippet on top, and indentation is bad on second.

Then check out these links to understand the use of setTimeout.

https://developer.mozilla.org/en/DOM/window.setTimeout

http://www.w3schools.com/jsref/met_win_settimeout.asp

And to your question :

if you put var t = before setTimeout(function() { it would work as expected,

Hope these helps, Sinan.

Sinan Y.
why should it work when i make the setTimeout to a var named "t" ?
Karem