javascript 注意 Jquery 插件超时没有发生

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11524139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 13:31:07  来源:igfitidea点击:

noty Jquery plugin timeout not happening

javascriptjqueryjquery-pluginsnotificationsnoty

提问by Rohit Kumar

The Jquery noty plugin timeout is not working when fed with a list of messages. I get the list of messages from servlet and call noty like this.

当输入消息列表时,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>

Ideally this should loop over the messages and print them with a timeout of 5000ms. But this prints all of the messages at once. I further tried to use the javascript native setTimeout function and replaced my callNotification with this.

理想情况下,这应该遍历消息并以 5000 毫秒的超时时间打印它们。但这会一次打印所有消息。我进一步尝试使用 javascript 本机 setTimeout 函数并用这个替换了我的 callNotification 。

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

But this also proved ineffective. Strangely the timeout seems to work fine when I replace "layout":centerin notify method. Where am I going wrong. I want the messages to be displayed with the time out of 5 seconds after which the first message gets automatically erased and the next shows up.

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

回答by Gavin

Noty is coded so that if you have buttons in your Noty, it disables the timeout. That doesn't make sense to me, but that's how it is.

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

This is the culprit (line 62):

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

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

Just remove this.options.timeout = false;and that will keep the timeout working if you have a Noty with buttons.

this.options.timeout = false;如果您有带按钮的 Noty,只需删除即可保持超时正常工作。

回答by BuiltByPeter

To get this working I changed the following in jquery.noty.js ...

为了让它工作,我在 jquery.noty.js 中更改了以下内容......

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

to this ...

对这个...

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

回答by Aemal Sayer

My answer is for v2.3.7.

我的回答是针对 v2.3.7。

Noty returns a javascript object, hence if you check it on firebug by doing console.dir(n), you will find all the methods and properties of the returned object.

Noty 返回一个 javascript 对象,因此如果您通过执行 console.dir(n) 在 firebug 上检查它,您将找到返回对象的所有方法和属性。

Following will set 3 seconds timeout:

以下将设置 3 秒超时:

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

回答by Aitazaz Khan

Try closeWithwith timeout option hope it will work fine

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

回答by iDreams Chandrashekhar

You need to provide this option as a parameter: "buttons: false"

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

回答by Panda

You can also achieve this by specifying this option during initialization

您也可以通过在初始化期间指定此选项来实现此目的

buttons: false

More here http://ned.im/noty/#/about

更多在这里http://ned.im/noty/#/about

回答by iniravpatel

In your jquery.noty.packaged.js create a global variable named "master_self" file.Now in line 103 or 104 there must be a line var self = this;just below that line assign master_self as self master_self = self;

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

Now create a function in the same file:

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

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

Now call this function explicitly on the notification which you want to timeout in this way just after calling the noty object for creating notification:

现在,在调用用于创建通知的 noty 对象之后,以这种方式在通知上显式调用此函数:

autoTimeout(1000);