diff --git a/src/snackbar/snackbar.js b/src/snackbar/snackbar.js index 7a855abf..92a2a523 100644 --- a/src/snackbar/snackbar.js +++ b/src/snackbar/snackbar.js @@ -38,6 +38,7 @@ this.actionHandler_ = undefined; this.message_ = undefined; this.actionText_ = undefined; + this.timeoutID_ = undefined; this.queuedNotifications_ = []; this.setActionHidden_(true); }; @@ -86,7 +87,7 @@ this.textElement_.textContent = this.message_; this.element_.classList.add(this.cssClasses_.ACTIVE); this.element_.setAttribute('aria-hidden', 'false'); - setTimeout(this.cleanup_.bind(this), this.timeout_); + this.timeoutID_ = setTimeout(this.cleanup_.bind(this), this.timeout_); }; @@ -127,7 +128,21 @@ } }; MaterialSnackbar.prototype['showSnackbar'] = MaterialSnackbar.prototype.showSnackbar; - + /** + * Hide the snackbar. + * + * @public + */ + MaterialSnackbar.prototype.hideSnackbar = function() { + if (!this.active) { + return; + } + if (typeof this.timeoutID_ === 'number') { + clearTimeout(this.timeoutID_); + this.cleanup_(); + } + }; + MaterialSnackbar.prototype['hideSnackbar'] = MaterialSnackbar.prototype.hideSnackbar; /** * Check if the queue has items within it. * If it does, display the next entry. @@ -158,6 +173,7 @@ this.actionHandler_ = undefined; this.message_ = undefined; this.actionText_ = undefined; + this.timeoutID_ = undefined; this.active = false; this.checkQueue_(); }.bind(this), /** @type {number} */ (this.Constant_.ANIMATION_LENGTH));