jQuery:带有通知功能的超时/延迟?

发布于 2025-01-04 15:37:01 字数 938 浏览 0 评论 0原文

我编写了以下简单函数,该函数采用两个参数,这些参数主要来自另一个函数,该函数从服务器返回 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 技术交流群。

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

发布评论

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

评论(4

暖风昔人 2025-01-11 15:37:01

那里没有什么明显的东西。我会尝试修剪代码,直到它按预期滑落。删除回调,删除 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.

凶凌 2025-01-11 15:37:01

试试这个

note.slideDown().delay(timing).slideUp();

Try this

note.slideDown().delay(timing).slideUp();
森林很绿却致人迷途 2025-01-11 15:37:01

我认为问题可能出在slideDown使用的easing函数中,默认情况下是swing(对数)。尝试使用线性,也许尝试使用更快的下滑时间

note.slideDown(400, 'linear').delay(timing).slideUp(400, 'linear');

i think that the problem might be in the easing function used by the slideDown which is swing by default (which is logaritmic). try using linear and maybe try using a faster slidedown time

note.slideDown(400, 'linear').delay(timing).slideUp(400, 'linear');
空城仅有旧梦在 2025-01-11 15:37:01

您没有将 duration 值传递给 slideDown。尝试:

note.slideDown(1000, function() {
  note.delay(timing).slideUp();
});

You are not passing a value for duration to slideDown. Try:

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