aboutsummaryrefslogtreecommitdiffstats
path: root/src/js/utils.js
diff options
context:
space:
mode:
authorSam Potts <sam@potts.es>2018-03-22 00:04:28 +1100
committerSam Potts <sam@potts.es>2018-03-22 00:04:28 +1100
commit59d4a27240e77c7ee586829b1d5f032c912a1d87 (patch)
tree3b090d636dbe3048437bfbd34b64c3f6ed88c722 /src/js/utils.js
parentbb7eea27e513bf33dd4a4d846b3234b37a5be48d (diff)
downloadplyr-59d4a27240e77c7ee586829b1d5f032c912a1d87.tar.lz
plyr-59d4a27240e77c7ee586829b1d5f032c912a1d87.tar.xz
plyr-59d4a27240e77c7ee586829b1d5f032c912a1d87.zip
Improve Sprite checking (fixes #827)
Diffstat (limited to 'src/js/utils.js')
-rw-r--r--src/js/utils.js17
1 files changed, 12 insertions, 5 deletions
diff --git a/src/js/utils.js b/src/js/utils.js
index 593e2bca..1d23172f 100644
--- a/src/js/utils.js
+++ b/src/js/utils.js
@@ -143,7 +143,14 @@ const utils = {
const hasId = utils.is.string(id);
let isCached = false;
- function updateSprite(data) {
+ const exists = () => document.querySelectorAll(`#${id}`).length;
+
+ function injectSprite(data) {
+ // Check again incase of race condition
+ if (hasId && exists()) {
+ return;
+ }
+
// Inject content
this.innerHTML = data;
@@ -151,8 +158,8 @@ const utils = {
document.body.insertBefore(this, document.body.childNodes[0]);
}
- // Only load once
- if (!hasId || !document.querySelectorAll(`#${id}`).length) {
+ // Only load once if ID set
+ if (!hasId || !exists()) {
// Create container
const container = document.createElement('div');
utils.toggleHidden(container, true);
@@ -168,7 +175,7 @@ const utils = {
if (isCached) {
const data = JSON.parse(cached);
- updateSprite.call(container, data.content);
+ injectSprite.call(container, data.content);
return;
}
}
@@ -190,7 +197,7 @@ const utils = {
);
}
- updateSprite.call(container, result);
+ injectSprite.call(container, result);
})
.catch(() => {});
}