|
2 | 2 | * SimpleToast - A small library for toasts |
3 | 3 | */ |
4 | 4 | (() => { |
5 | | - const version = buildVersion(1, 3); |
| 5 | + const version = buildVersion(1, 4); |
6 | 6 | if (window.SimpleToast) { |
7 | 7 | if (SimpleToast.version) { |
8 | 8 | if (SimpleToast.version >= version.number) return; |
|
95 | 95 | } |
96 | 96 | return el; |
97 | 97 | } |
| 98 | + return document.getElementById('AlertToast') || create(); |
| 99 | + })(); |
| 100 | + let count = 0; |
98 | 101 |
|
99 | | - setInterval(() => { // TODO: don't always run a timer |
| 102 | + let timeout = null; |
| 103 | + function startTimeout() { |
| 104 | + if (timeout) return; |
| 105 | + timeout = setTimeout(() => { |
| 106 | + timeout = null; |
100 | 107 | const now = Date.now(); |
| 108 | + let pending = 0; |
101 | 109 | toasts.forEach((toast) => { |
102 | | - if (!toast.timeout || now < toast.timeout) return; |
103 | | - // Close toast |
| 110 | + if (!toast.timeout) return; |
| 111 | + if (now < toast.timeout) { |
| 112 | + pending += 1; |
| 113 | + return; |
| 114 | + } |
104 | 115 | toast.close(); |
105 | 116 | }); |
| 117 | + if (pending) { |
| 118 | + startTimeout(); |
| 119 | + } |
106 | 120 | }, 1000); |
107 | | - return document.getElementById('AlertToast') || create(); |
108 | | - })(); |
109 | | - let count = 0; |
| 121 | + } |
110 | 122 |
|
111 | 123 | function Toast({title, text, className, css = {}, buttons, timeout}) { |
112 | 124 | if (typeof arguments[0] === 'string') { |
|
151 | 163 | if (!button.text) return; |
152 | 164 | const elb = document.createElement('button'); |
153 | 165 | if (button.className || className && className.button) { |
154 | | - const clazz = button.className || className.button |
| 166 | + const clazz = button.className || className.button; |
155 | 167 | elb.className = Array.isArray(clazz) ? clazz.join(',') : clazz; |
156 | 168 | } |
157 | 169 | elb.innerHTML = button.text; |
|
182 | 194 |
|
183 | 195 | root.appendChild(el); |
184 | 196 | toasts.set(id, toast); |
| 197 | + if (timeout) { |
| 198 | + startTimeout(); |
| 199 | + } |
185 | 200 | return toast; |
186 | 201 | } |
187 | 202 |
|
|
0 commit comments