Jquery 一定时间后调用函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7710464/
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-08-27 00:11:51  来源:igfitidea点击:

Call a function after a certain time Jquery?

jqueryfunctiontime

提问by kamaci

I am examining a js file form Bram Jetten:

我正在检查一个 js 文件形式 Bram Jetten:

Notification.fn = Notification.prototype;

function Notification(value, type, tag) {
  this.log(value, type);
  this.element = $('<li><span class="image '+ type +'"></span>' + value + '</li>');
  if(typeof tag !== "undefined") {
    $(this.element).append('<span class="tag">' + tag + '</span>');
  }
  $("#notifications").append(this.element);
  this.show();
}

/**
 * Show notification
 */
Notification.fn.show = function() {
  $(this.element).slideDown(200);
  $(this.element).click(this.hide);
}

/**
 * Hide notification
 */
Notification.fn.hide = function() {  
  $(this).animate({opacity: .01}, 200, function() {
    $(this).slideUp(200, function() {
      $(this).remove();
    });
  });
}

...

I assigned a click event one of my buttons and when I click that button it calls a new notification:

我为我的按钮之一分配了一个单击事件,当我单击该按钮时,它会调用一个新通知:

new Notification('Hi', 'success');

When I click that notification it closes as well. However if I dont click it after a certain time I want it close by itself. How can I call that hide function or close it after some time later when it appeared?

当我单击该通知时,它也会关闭。但是,如果我在一段时间后不单击它,我希望它自己关闭。我如何调用隐藏函数或在它出现一段时间后关闭它?

回答by kamaci

var that = this;

setTimeout(function() {   //calls click event after a certain time
   that.element.click();
}, 10000);

that worked for me.

这对我有用。

回答by Lapple

Set a timeout and force the hide.

设置超时并强制隐藏。

/**
 * Show notification
 */
Notification.fn.show = function() {
  var self = this;
  $(self.element).slideDown(200)
                 .click(self.hide);

  setTimeout(function() {
    self.hide();
    // 3000 for 3 seconds
  }, 3000)
}

回答by japrescott

Change lines to

将行更改为

Notification.fn.show = function() {
    var self=this;
    $(this.element).slideDown(200);
    $(this.element).click(this.hide);
    setTimeout(function(){
        self.hide();
    },2000);
}

but you will need an additional internal boolean, so that you cant hide (and therfor destroy) the notification twice.

但是您将需要一个额外的内部布尔值,以便您无法隐藏(以及因此销毁)通知两次。

Notification.fn.hide = function() {
  if (!this.isHidden){  
    var self=this;
    $(this).animate({opacity: .01}, 200, function() {
      $(this).slideUp(200, function() {
        $(this).remove();
        self.isHidden=true;
      });
    });
  }
}

回答by user3821656

calls click eventafter a certain time

click event一定时间后 调用

setTimeout(function() {   //calls click event after a certain time
      $(".signature-container .nf-field-element").append( $('#signature-pad')); 
}, 10000);