Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear timeout before showing the toast #31155

Merged
merged 5 commits into from Jul 11, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 10 additions & 2 deletions js/src/toast.js
Expand Up @@ -91,6 +91,8 @@ class Toast {
return
}

this._clearTimeout()

if (this._config.animation) {
this._element.classList.add(CLASS_NAME_FADE)
}
Expand Down Expand Up @@ -149,8 +151,7 @@ class Toast {
}

dispose() {
clearTimeout(this._timeout)
this._timeout = null
this._clearTimeout()

if (this._element.classList.contains(CLASS_NAME_SHOW)) {
this._element.classList.remove(CLASS_NAME_SHOW)
Expand Down Expand Up @@ -186,6 +187,13 @@ class Toast {
)
}

_clearTimeout() {
if (this._timeout) {
rohit2sharma95 marked this conversation as resolved.
Show resolved Hide resolved
clearTimeout(this._timeout)
this._timeout = null
}
}

// Static

static jQueryInterface(config) {
Expand Down
24 changes: 24 additions & 0 deletions js/tests/unit/toast.spec.js
Expand Up @@ -170,6 +170,30 @@ describe('Toast', () => {

toast.show()
})

it('should clear timeout if toast is shown again before it is hidden', done => {
fixtureEl.innerHTML = [
'<div class="toast">',
' <div class="toast-body">',
' a simple toast',
' </div>',
'</div>'
].join('')

const toastEl = fixtureEl.querySelector('.toast')
const toast = new Toast(toastEl)

setTimeout(() => {
toast._config.autohide = false
toastEl.addEventListener('shown.bs.toast', () => {
expect(toast._timeout).toBeNull()
done()
})
toast.show()
}, toast._config.delay / 2)

toast.show()
rohit2sharma95 marked this conversation as resolved.
Show resolved Hide resolved
})
})

describe('hide', () => {
Expand Down