I'm on Safari 12.0.3. I have:
function doThisOnlyOnce() {
if (MyNamespace.firstUserGesture) {
console.log('Do stuff only once!')
}
MyNamespace.firstUserGesture = false
}
function init() {
window.MyNamespace = {} // Easy access to some vars.
MyNamespace.firstUserGesture = true
MyNamespace.vid = document.getElementById("vid")
MyNamespace.vid.addEventListener('click', doThisOnlyOnce)
}
<body onload="init()">
<div id="vid">x</div>
</body>
On Safari, doThisOnlyOnce() is run on every click. MyNamespace.firstUserGesture is not set to false at all.
On any other browser doThisOnlyOnce() is run on the first click only, as expected.
What am I missing?
Going through the code line-by-line with Safari JS debugger indicates that one of the statements of doThisOnlyOnce function was throwing an exception. Removing the exception cause, also removed this strange behavior too.