Skip to content

Commit d8ee292

Browse files
add timeout, ensure multiple attempts at adding, limit var scope
1 parent 06ee42f commit d8ee292

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

fathom-data-attr.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
1-
const spaValue = "auto";
1+
(function(){
2+
let attempts = 0;
23

3-
function addDataAttr() {
4-
const el = document.getElementById("fathom-script");
5-
const dataSpaValue = el.dataset.spa;
4+
function addDataAttr() {
5+
const spaValue = "auto";
6+
const el = document.getElementById("fathom-script");
7+
attempts += 1;
8+
9+
if (!el && attempts < 10) {
10+
window.setTimeout(addDataAttr, 100);
11+
return;
12+
}
613

7-
if (dataSpaValue === undefined) {
8-
el.dataset.spa = spaValue;
14+
const dataSpaValue = el.dataset.spa;
15+
16+
if (dataSpaValue === undefined) {
17+
el.dataset.spa = spaValue;
18+
}
919
}
10-
}
1120

12-
addDataAttr();
21+
addDataAttr();
22+
})();

0 commit comments

Comments
 (0)