Skip to content

Commit 67e7308

Browse files
committed
Restart the progress bar after re-focusing the page (apvarun#109, apvarun#112)
1 parent a429134 commit 67e7308

File tree

2 files changed

+63
-37
lines changed

2 files changed

+63
-37
lines changed

src/toastify-es.js

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,33 @@ class Toastify {
137137
return this;
138138
}
139139

140+
startToast() {
141+
var self = this;
142+
// add back the timeout
143+
this.toastElement.timeOutValue = window.setTimeout(
144+
() => {
145+
// Remove the toast from DOM
146+
this._removeElement(this.toastElement);
147+
},
148+
this.options.duration
149+
);
150+
151+
// Restart the progress bar animation
152+
var progressBar = this.toastElement.querySelector('.toast-progress');
153+
progressBar.style.animationPlayState = '';
154+
var newProgressBar = progressBar.cloneNode(true);
155+
progressBar.parentNode.replaceChild(newProgressBar, progressBar);
156+
}
157+
158+
pauseToast() {
159+
// Stop countdown
160+
window.clearTimeout(this.toastElement.timeOutValue);
161+
162+
// Pause the progress bar animation
163+
var progressBar = this.toastElement.querySelector('.toast-progress');
164+
progressBar.style.animationPlayState = 'paused';
165+
}
166+
140167
/**
141168
* Hide the toast
142169
* @public
@@ -291,20 +318,14 @@ class Toastify {
291318
divElement.addEventListener(
292319
"mouseover",
293320
(event) => {
294-
window.clearTimeout(divElement.timeOutValue);
321+
this.pauseToast();
295322
}
296323
)
297324
// add back the timeout
298325
divElement.addEventListener(
299326
"mouseleave",
300327
() => {
301-
divElement.timeOutValue = window.setTimeout(
302-
() => {
303-
// Remove the toast from DOM
304-
this._removeElement(divElement);
305-
},
306-
this.options.duration
307-
)
328+
this.startToast();
308329
}
309330
)
310331
}
@@ -315,17 +336,9 @@ class Toastify {
315336
"visibilitychange",
316337
(event) => {
317338
if (document.visibilityState === 'visible') {
318-
// add back the timeout
319-
divElement.timeOutValue = window.setTimeout(
320-
() => {
321-
// Remove the toast from DOM
322-
this._removeElement(divElement);
323-
},
324-
this.options.duration
325-
)
339+
this.startToast();
326340
} else {
327-
// stop countdown
328-
window.clearTimeout(divElement.timeOutValue);
341+
this.pauseToast();
329342
}
330343
}
331344
)

src/toastify.js

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -210,21 +210,15 @@
210210
// stop countdown
211211
divElement.addEventListener(
212212
"mouseover",
213-
function(event) {
214-
window.clearTimeout(divElement.timeOutValue);
213+
function() {
214+
self.pauseToast();
215215
}
216216
)
217217
// add back the timeout
218218
divElement.addEventListener(
219219
"mouseleave",
220220
function() {
221-
divElement.timeOutValue = window.setTimeout(
222-
function() {
223-
// Remove the toast from DOM
224-
self.removeElement(divElement);
225-
},
226-
self.options.duration
227-
)
221+
self.startToast();
228222
}
229223
)
230224
}
@@ -236,17 +230,9 @@
236230
"visibilitychange",
237231
function() {
238232
if (document.visibilityState === 'visible') {
239-
// add back the timeout
240-
divElement.timeOutValue = window.setTimeout(
241-
function() {
242-
// Remove the toast from DOM
243-
self.removeElement(divElement);
244-
},
245-
self.options.duration
246-
)
233+
self.startToast();
247234
} else {
248-
// stop countdown
249-
window.clearTimeout(divElement.timeOutValue);
235+
self.pauseToast();
250236
}
251237
}
252238
)
@@ -343,6 +329,33 @@
343329
return this;
344330
},
345331

332+
startToast: function() {
333+
var self = this;
334+
// add back the timeout
335+
this.toastElement.timeOutValue = window.setTimeout(
336+
function() {
337+
// Remove the toast from DOM
338+
self.removeElement(self.toastElement);
339+
},
340+
self.options.duration
341+
);
342+
343+
// Restart the progress bar animation
344+
var progressBar = this.toastElement.querySelector('.toast-progress');
345+
progressBar.style.animationPlayState = '';
346+
var newProgressBar = progressBar.cloneNode(true);
347+
progressBar.parentNode.replaceChild(newProgressBar, progressBar);
348+
},
349+
350+
pauseToast: function() {
351+
// Stop countdown
352+
window.clearTimeout(this.toastElement.timeOutValue);
353+
354+
// Pause the progress bar animation
355+
var progressBar = this.toastElement.querySelector('.toast-progress');
356+
progressBar.style.animationPlayState = 'paused';
357+
},
358+
346359
hideToast: function() {
347360
if (this.toastElement.timeOutValue) {
348361
clearTimeout(this.toastElement.timeOutValue);

0 commit comments

Comments
 (0)