--- title: Notification.close() slug: Web/API/notification/close tags: - Notification.close() translation_of: Web/API/Notification/close ---
{{APIRef("Web Notifications")}}

{{domxref("Notification")}} 接口的 close() 的方法用于关闭一个以前显示的通知。

Syntax

Notification.close();

Parameters

None.

Returns

Void.

Examples

以下是 Emogotchi 示例在线演示)中的一段代码 ,定义了一个简单的函数spawnNotification,当spawnNotification被调用时会创建一个对象并生成一个新的Notification。在函数的最后,它在{{domxref("WindowTimers.setTimeout","setTimeout()")}} 中调用了close()函数来实现在4s后关闭Notification(有些浏览器会自动关闭弹出的Notification,但有些不是,例如Chrome,Opera)。还要注意bind()的使用,来确保close()方法绑定到Notification的实例上。

function spawnNotification(theBody,theIcon,theTitle) {
  var options = {
      body: theBody,
      icon: theIcon
  }

  var n = new Notification(theTitle,options);
  setTimeout(n.close.bind(n), 4000);
}

Specifications

Specification Status Comment
{{SpecName('Web Notifications')}} {{Spec2('Web Notifications')}} Living standard

Browser compatibility

{{Compat("api.Notification.close")}}

See also