Skip to content

Commit 24c8a48

Browse files
Fix: remove interval handler
Because I’m a JS newbie, I didn’t notice that setting the event listener to just running *once* means that the interval handler will get automagically removed — no need for the extra trouble! Ancient browsers might not like it, but the code won’t run on ancient browsers anyway…
1 parent 33d1576 commit 24c8a48

File tree

1 file changed

+18
-19
lines changed

1 file changed

+18
-19
lines changed

gwynethllewelyn/postlocalstorage/styles/all/template/postlocalstorage_functions.js

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,6 @@
1414
* @param {Document} doc - This object's DOM (will be set to `this.document`).
1515
*/
1616
(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-
2417
/**
2518
* Freshness interval in milliseconds.
2619
* One year = 31536000000 milliseconds.
@@ -130,7 +123,7 @@
130123
*
131124
* It gets triggered by the "type" events on the input and textarea elements,
132125
* @function updateStorage
133-
* @type EventListener
126+
* @type {EventListener}
134127
*/
135128
function updateStorage() {
136129
// Note: if the visibilitychange event is being used, one should check to see if the visibilityState
@@ -146,40 +139,46 @@
146139
message.localStorage.removeItem(key);
147140
console.debug("Empty textarea -- remove existing content & subject in localStorage");
148141
}
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);
152142
}
153143
// 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.
154145
textarea.addEventListener(
155146
"keyup",
156-
/** @listens keyup */
147+
/**
148+
* @listens keyup
149+
* @type {EventListener}
150+
*/
157151
function() {
158152
message.addEventListener(unloadEvent, updateStorage);
159-
nIntervId = message.setInterval(updateStorage, 10000);
153+
message.setInterval(updateStorage, 10000);
160154
}, { once: true }
161155
);
162156
// Same for the subject, I think:
163157
subject.addEventListener(
164158
"keyup",
165-
/** @listens keyup */
159+
/**
160+
* @listens keyup
161+
* @type {EventListener}
162+
*/
166163
function() {
167164
message.addEventListener(unloadEvent, updateStorage);
168-
nIntervId = message.setInterval(updateStorage, 10000);
165+
message.setInterval(updateStorage, 10000);
169166
}, { once: true }
170167
);
171168

172169
// When the form is submitted, delete the localStorage key/value pair.
173170
textarea.form.addEventListener(
174171
"submit",
175-
/** @listens submit */
172+
/**
173+
* @listens submit
174+
* @type {EventListener}
175+
*/
176176
function() {
177177
// ... 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
180180
message.localStorage.removeItem(key);
181181
message.removeEventListener(unloadEvent, updateStorage);
182-
message.clearInterval(nIntervId);
183182
console.debug("Text submitted (not in preview!); removed from localStorage");
184183
}
185184
}

0 commit comments

Comments
 (0)