|
14 | 14 | * @param {Document} doc - This object's DOM (will be set to `this.document`). |
15 | 15 | */ |
16 | 16 | (function(message, doc) { |
17 | | - /** |
18 | | - * Handler for the SetInterval event, so we can remove it on submit. |
19 | | - * @type {number} |
20 | | - * @since 1.1.0 |
21 | | - */ |
22 | | - var nIntervId; |
23 | | - |
24 | 17 | /** |
25 | 18 | * Freshness interval in milliseconds. |
26 | 19 | * One year = 31536000000 milliseconds. |
|
130 | 123 | * |
131 | 124 | * It gets triggered by the "type" events on the input and textarea elements, |
132 | 125 | * @function updateStorage |
133 | | - * @type EventListener |
| 126 | + * @type {EventListener} |
134 | 127 | */ |
135 | 128 | function updateStorage() { |
136 | 129 | // Note: if the visibilitychange event is being used, one should check to see if the visibilityState |
|
146 | 139 | message.localStorage.removeItem(key); |
147 | 140 | console.debug("Empty textarea -- remove existing content & subject in localStorage"); |
148 | 141 | } |
149 | | - // This event listener is no longer needed now so remove it. |
150 | | - // Once the user presses another key, it will be added again! |
151 | | - message.removeEventListener(unloadEvent, updateStorage); |
152 | 142 | } |
153 | 143 | // When the user presses a key just *once* inside the textarea, run the storage function when the page is unloaded. |
| 144 | + // Note that because this happens just once, the listener is removed afterwards and we don't need to track it. |
154 | 145 | textarea.addEventListener( |
155 | 146 | "keyup", |
156 | | - /** @listens keyup */ |
| 147 | + /** |
| 148 | + * @listens keyup |
| 149 | + * @type {EventListener} |
| 150 | + */ |
157 | 151 | function() { |
158 | 152 | message.addEventListener(unloadEvent, updateStorage); |
159 | | - nIntervId = message.setInterval(updateStorage, 10000); |
| 153 | + message.setInterval(updateStorage, 10000); |
160 | 154 | }, { once: true } |
161 | 155 | ); |
162 | 156 | // Same for the subject, I think: |
163 | 157 | subject.addEventListener( |
164 | 158 | "keyup", |
165 | | - /** @listens keyup */ |
| 159 | + /** |
| 160 | + * @listens keyup |
| 161 | + * @type {EventListener} |
| 162 | + */ |
166 | 163 | function() { |
167 | 164 | message.addEventListener(unloadEvent, updateStorage); |
168 | | - nIntervId = message.setInterval(updateStorage, 10000); |
| 165 | + message.setInterval(updateStorage, 10000); |
169 | 166 | }, { once: true } |
170 | 167 | ); |
171 | 168 |
|
172 | 169 | // When the form is submitted, delete the localStorage key/value pair. |
173 | 170 | textarea.form.addEventListener( |
174 | 171 | "submit", |
175 | | - /** @listens submit */ |
| 172 | + /** |
| 173 | + * @listens submit |
| 174 | + * @type {EventListener} |
| 175 | + */ |
176 | 176 | function() { |
177 | 177 | // ... except on Preview. We still want to keep the storage around during preview! |
178 | | - // Kudos to |
179 | | - if (document.activeElement.value != 'Preview') { |
| 178 | + // Kudos to @kylesands for this |
| 179 | + if (document.activeElement.tagName.toLowerCase() == "input" && document.activeElement.value.toLowerCase() == 'submit') { // Added to only clear on Input button with Submit value |
180 | 180 | message.localStorage.removeItem(key); |
181 | 181 | message.removeEventListener(unloadEvent, updateStorage); |
182 | | - message.clearInterval(nIntervId); |
183 | 182 | console.debug("Text submitted (not in preview!); removed from localStorage"); |
184 | 183 | } |
185 | 184 | } |
|
0 commit comments