在哪里放置 timeout() 淡出?

发布于 2024-08-24 07:54:15 字数 943 浏览 2 评论 0原文

函数检查会话(){ $.ajax({url: "session.php", 成功: 函数(数据){ 如果(数据== 1){ var postFilen = 'msg.php'; $.post(postFilen, 函数(数据){ $(".msg").html(data).find(".message2").fadeIn("慢") }别的{ $('.msg').hide(); } }}); // setInterval('checkSession()',1000);

现在我想在显示 5 秒后淡出 .msg。 我该怎么办这个.. 我已经尝试过:

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);
}

但是第一次之后该消息将不会出现..

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..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

给我一枪 2024-08-31 07:54:15

尝试使用 delay 方法: http://api.jquery.com/延迟/

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

我的情况,类似于:

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

[编辑:固定示例]

[编辑2:新示例]


这个似乎在这里工作正常:

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

然后

   $(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');
      }) 
   });

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');
      }) 
   });
葮薆情 2024-08-31 07:54:15

首先尝试编写更清晰的代码,以便我们更好地理解您的问题,您的 $.post 的回调函数缺少 }); 这些字符位于顶部第一个片段的末尾,并且第二个缩进很糟糕。

然后查看这些链接以了解 setTimeout 的用法。

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

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

对于你的问题:

如果你把var t =放在setTimeout(function() {之前)它会按预期工作,

希望这些有所帮助,Sinan。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文