Created
September 17, 2021 01:21
-
-
Save itsjohncs/f95df23cc40adf1c2a3fe761e52733c9 to your computer and use it in GitHub Desktop.
One works and the other freezes the browser... Why?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<script> | |
let _openCVPromise = null; | |
function getOpenCV() { | |
if (_openCVPromise === null) { | |
_openCVPromise = new Promise(function(resolve, reject) { | |
const script = document.createElement("script"); | |
script.setAttribute("src", "https://shmeppy-external-js.sfo3.cdn.digitaloceanspaces.com/opencv-4.5.3.js"); | |
script.addEventListener("load", function() { | |
resolve(window.cv); // !! Only difference | |
}); | |
document.head.appendChild(script); | |
}); | |
} | |
return _openCVPromise; | |
} | |
</script> | |
</head> | |
<body> | |
<button onClick="console.log(getOpenCV())">load</button> | |
<button onClick="console.log('OpenCV loaded', !!window.cv)">Beep</button> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<script> | |
let _openCVPromise = null; | |
function getOpenCV() { | |
if (_openCVPromise === null) { | |
_openCVPromise = new Promise(function(resolve, reject) { | |
const script = document.createElement("script"); | |
script.setAttribute("src", "https://shmeppy-external-js.sfo3.cdn.digitaloceanspaces.com/opencv-4.5.3.js"); | |
script.addEventListener("load", function() { | |
resolve(); // !! Only difference | |
}); | |
document.head.appendChild(script); | |
}); | |
} | |
return _openCVPromise; | |
} | |
</script> | |
</head> | |
<body> | |
<button onClick="console.log(getOpenCV())">load</button> | |
<button onClick="console.log('OpenCV loaded', !!window.cv)">Beep</button> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ahhhh that makes sense. Thank you for solving this mystery.
Though I have a new mystery. I forget why I made this gist. Where did you find a link to this?