jQuery:带有通知功能的超时/延迟?
我编写了以下简单函数,该函数采用两个参数,这些参数主要来自另一个函数,该函数从服务器返回 json。
var timing = 10000;
function notificationOutput(type, message) {
console.log('output now!');
var note = $('.notification');
note.css('display', 'none');
if ( type == "success" ) { note.removeClass('warning').addClass('success'); }
if ( type == "warning" ) { note.removeClass('success').addClass('warning'); }
note.find('.message').html(message);
note.slideDown( function() {
note.delay(timing).slideUp();
});
}
它所做的只是从页面顶部滑下一个栏,发出一条消息(成功或警告)。 timing
变量用于通知栏停留 10 秒。因此,当该函数被触发时,我希望条形图 slideDown()
保持该位置 10 秒,然后再次 slideUp()
。
然而,现在当该功能被触发时,会发生奇怪的超时,直到通知栏出现。这意味着当该函数被触发时,我现在的 console.log()
输出会立即记录在我的 JS 控制台中,但 slipDown() 需要多花几秒钟的时间才能出现!这是为什么?
我希望 slideDown()
立即发生(与 output now
记录在控制台的同时)。为什么会出现延迟?
感谢您的帮助!
I wrote the following simple function that takes two parameters mostly coming from another function returned with json from the server.
var timing = 10000;
function notificationOutput(type, message) {
console.log('output now!');
var note = $('.notification');
note.css('display', 'none');
if ( type == "success" ) { note.removeClass('warning').addClass('success'); }
if ( type == "warning" ) { note.removeClass('success').addClass('warning'); }
note.find('.message').html(message);
note.slideDown( function() {
note.delay(timing).slideUp();
});
}
All it does is simply sliding down a bar from the top of my page putting out a message (either success or warning). The timing
variable is for the notification-bar to stay for 10 seconds. So when the function is triggered I want the bar to slideDown()
, hold that position for 10seconds and than slideUp()
again.
However right now when the function is triggered there is a weird timeout happening till the notification bar appears. That means when the function is fired the console.log()
output I have in there right now is logged immediately in my JS-console but the slideDown() takes a few seconds longer to appear! Why is that?
I want the slideDown()
to happen immediately (at the same time as the output now
is logged in the console). Why is there a delay happening?
Thanks for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
那里没有什么明显的东西。我会尝试修剪代码,直到它按预期滑落。删除回调,删除 html-set,删除 success/warning 类设置器,在输出到控制台之前选择 note-element,用立即显示替换幻灯片等。
还可以尝试调用
.stop(true ,true)
首先在注释上:note.stop(true,true).slideDown();
。这是为了防止它正忙于处理其他动画并且向下滑动正在排队。Nothing obvious there. I would try trimming the code down until it slides down as expected. Remove the callback, remove the html-set, remove the success/warning class-setters, select the note-element before outputting to the console, replace the slide with an immediate show, etc.
Also try calling
.stop(true,true)
on the note first:note.stop(true,true).slideDown();
. This is in case it is busy with some other animation and the slide down is being queued.试试这个
Try this
我认为问题可能出在slideDown使用的
easing
函数中,默认情况下是swing
(对数)。尝试使用线性,也许尝试使用更快的下滑时间i think that the problem might be in the
easing
function used by the slideDown which isswing
by default (which is logaritmic). try using linear and maybe try using a faster slidedown time您没有将
duration
值传递给slideDown
。尝试:You are not passing a value for
duration
toslideDown
. Try: