在哪里放置 timeout() 淡出?
函数检查会话(){ $.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试使用
delay
方法: http://api.jquery.com/延迟/我的情况,类似于:
[编辑:固定示例]
[编辑2:新示例]
这个似乎在这里工作正常:
然后
Try to work with the
delay
method: http://api.jquery.com/delay/I your case, something like:
[edit: fixed example]
[edit2: new example]
This one seems to work fine here:
and then
首先尝试编写更清晰的代码,以便我们更好地理解您的问题,您的
$.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 =
beforesetTimeout(function() {
it would work as expected,Hope these helps, Sinan.