|
22 | 22 | * @property {url} avatar - Image/icon to be shown before text |
23 | 23 | * @property {string} className - Ability to provide custom class name for further customization |
24 | 24 | * @property {boolean} stopOnFocus - To stop timer when hovered over the toast (Only if duration is set) |
| 25 | + * @property {boolean} stopOnWindowBlur - To stop timer if window is not focused (Only if duration is set) |
25 | 26 | * @property {Function} callback - Invoked when the toast is dismissed |
26 | 27 | * @property {Function} onClick - Invoked when the toast is clicked |
27 | 28 | * @property {Object} offset - Ability to add some offset to axis |
@@ -50,6 +51,7 @@ class Toastify { |
50 | 51 | avatar: "", |
51 | 52 | className: "", |
52 | 53 | stopOnFocus: true, |
| 54 | + stopOnWindowBlur: true, |
53 | 55 | onClick: function() {}, |
54 | 56 | offset: { x: 0, y: 0 }, |
55 | 57 | escapeMarkup: true, |
@@ -158,6 +160,7 @@ class Toastify { |
158 | 160 | * @param {url} [options.avatar] - Image/icon to be shown before text |
159 | 161 | * @param {string} [options.className] - Ability to provide custom class name for further customization |
160 | 162 | * @param {boolean} [options.stopOnFocus] - To stop timer when hovered over the toast (Only if duration is set) |
| 163 | + * @param {boolean} [options.stopOnWindowBlur] - To stop timer if window is not focused (Only if duration is set) |
161 | 164 | * @param {Function} [options.callback] - Invoked when the toast is dismissed |
162 | 165 | * @param {Function} [options.onClick] - Invoked when the toast is clicked |
163 | 166 | * @param {Object} [options.offset] - Ability to add some offset to axis |
@@ -300,6 +303,28 @@ class Toastify { |
300 | 303 | ) |
301 | 304 | } |
302 | 305 |
|
| 306 | + // Clear timeout while window is not focused |
| 307 | + if (this.options.stopOnWindowBlur && this.options.duration > 0) { |
| 308 | + document.addEventListener( |
| 309 | + "visibilitychange", |
| 310 | + (event) => { |
| 311 | + if (document.visibilityState === 'visible') { |
| 312 | + // add back the timeout |
| 313 | + divElement.timeOutValue = window.setTimeout( |
| 314 | + () => { |
| 315 | + // Remove the toast from DOM |
| 316 | + this._removeElement(divElement); |
| 317 | + }, |
| 318 | + this.options.duration |
| 319 | + ) |
| 320 | + } else { |
| 321 | + // stop countdown |
| 322 | + window.clearTimeout(divElement.timeOutValue); |
| 323 | + } |
| 324 | + } |
| 325 | + ) |
| 326 | + } |
| 327 | + |
303 | 328 | // Adding an on-click destination path |
304 | 329 | if (typeof this.options.destination !== "undefined") { |
305 | 330 | divElement.addEventListener( |
|
0 commit comments