noty Jquery插件超时未发生

noty Jquery plugin timeout not happening

本文关键字:超时 Jquery 插件 noty      更新时间:2023-11-04

Jquery noty插件在收到消息列表时超时不起作用。我从servlet获得消息列表,并像这样调用noty。

<script>
    function callNotification()
    {
        <c:foreach var = "message" items = "${sessionScope.notification}">
             notify('${message}');
        </c:foreach>
    }
    function notify(message)
    {
       noty({
                "text": message,
                "theme": noty_theme_facebook",
                "layout": topRight,
                "information","animateOpen":{"height":"toggle"},
                "information","animateOpen":{"height":"toggle"},
                "speed":500,
                "timeout":5000,
                "closeButton":true,
                "closeOnSelfClick":true,
                "closeOnSelfOver":false,
                "modal":false
          })
    </script>

理想情况下,这应该在消息上循环并打印它们,超时时间为5000ms。但这会同时打印所有信息。我进一步尝试使用javascript本机setTimeout函数,并用它替换了我的callNotification。

 function callNotification()
    {
        <c:foreach var = "message" items = "${sessionScope.notification}">
         (function(message){ 
            setTimeout(function(){notify('${message}');},5000)
         }('${message}')
        }}
        </c:foreach>
    }

但这也被证明是无效的。奇怪的是,当我在notify方法中替换"layout":center时,超时似乎工作得很好。我哪里错了。我希望消息显示的时间超过5秒,之后第一条消息会自动删除,下一条消息会出现。

Noty经过编码,因此如果您的Noty中有按钮,它将禁用超时。这对我来说没有意义,但事情就是这样。

这就是罪魁祸首(第62行):

60: // If we have button disable closeWith & timeout options
61: this.options.closeWith = [];
62: this.options.timeout = false;

只要删除this.options.timeout = false;,如果你有一个带按钮的Noty,就会保持超时工作。

为了实现这一点,我在jquery.noty.js…中更改了以下内容

self.$bar.delay(self.options.timeout).promise().done(function () {
                self.close();
            });

对此。。。

setTimeout(function () {
                self.close();
            }, self.options.timeout);

我的答案是v2.3.7。

Noty返回一个javascript对象,因此,如果你通过console.dir(n)在firebug上检查它,你会发现返回对象的所有方法和属性。

以下将设置3秒超时:

var n = noty({text: 'noty - a jquery notification library!'});
n.setTimeout(3000);

尝试带有超时选项的closeWith,希望它能正常工作

   function generate(type, text) {
                var n = noty({
                    text        : text,
                    type        : type,
                    dismissQueue: true,
                    layout      : 'topRight',
                    closeWith   : ['click','timeout'],
                    theme       : 'relax',
                    maxVisible  : 10,
                    timeout     :7000,
                    animation   : {
                        open  : 'animated bounceInRight',
                        close : 'animated bounceOutRight',
                        easing: 'swing',
                        speed : 500
                    }
                });

您需要将此选项作为参数提供:"buttons:false"

在jquery.noty.packaged.js中创建一个名为"master_self"的全局变量文件。现在第103行或第104行必须有一行该行正下方的var self = this;将master _self分配为自身master_self = self;

现在在同一个文件中创建一个函数:

function autoTimeout(timeoutVal){
    setTimeout(function(){
                            var self = master_self;
                            if (typeof self.options.animation.close == 'string') {
                                self.$bar.addClass(self.options.animation.close).one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function() {
                                    if(self.options.callback.afterClose) self.options.callback.afterClose.apply(self);
                                    console.log(self);
                                    self.closeCleanUp();
                                });
                            } else {
                                self.$bar.clearQueue().stop().animate(
                                    self.options.animation.close,
                                    self.options.animation.speed,
                                    self.options.animation.easing,
                                    function() {
                                        if(self.options.callback.afterClose) self.options.callback.afterClose.apply(self);
                                    })
                                    .promise().done(function() {
                                        self.closeCleanUp();
                                    });
                            }
                         },timeoutVal);
}

现在,在调用noty对象创建通知后,在您想要以这种方式超时的通知上显式调用此函数:

autoTimeout(1000);

您也可以通过在初始化时指定此选项来实现这一点

buttons: false

点击此处了解更多信息http://ned.im/noty/#/about