That's right. I only load it once, as it's an event listener. It's loaded when the title screen is loaded, via the custom "home" function.
Here's the breakdown of the code...
document.addEventListener("visibilitychange", visible);
"document" means the whole HTML document. This section lets you be specific, by adding event listeners to specific elements in the document, but in this example it's an event listener for the whole document.
"addEventListener" ... this is a JavaScript thing. You're saying... "Hey, listen!"
"visibilitychange" ...this is the event you're listening for. It's a predetermined thing in JavaScript. There are many different types. Fortunately, there's one for determining if the page is visible.
"visible" ...This is the name of the function that is run whenever there is a change in visibility. It's a name I gave to the function. I could have called it Potato or Cauliflower, it doesn't matter. The point is that you'll need a function to call when there's a change in visibility.
Here's a line-by-line breakdown of the function...
// Look at me when I'm talking to you!
The double-slash means this is a comment. It's a note to yourself, or anyone else working with the code, as it can be a descriptive message as to what this part of the code does. I'm notorious for leaving goofy comments in my code, even use of emoji. Happy World Emoji Day.
function visible() {
Here's how a function is created. Notice how it's called "visible". This is the function that the event listener is going to run whenever the "visibilitychange" event occurs.
if (document.hidden) {
Here I'm using the read-only document property to see if the page is actually hidden. Document: hidden property - Web APIs | MDN ...but it could be reversed... if(!document.hidden) ...the exclamation point means "not". So if the document is not hidden, then do something. Since I only wanted to turn the speech off when the document is hidden, I didn't need to add other code.
speechSynthesis.cancel();
That's how I stop the voice sound effects. If I had background music, or other sound effects, then I could add additional code here. Like if I was using Howler.js... maybe I could fade out the sound. That's theoretical though. I have had significant trouble getting sound to work properly in my apps.
Ha ha great. Maybe leave a review to let other people know.
The more popular the app, the more likely it gets updated.
The new App Store Connect app update lets me see all the reviews for the app, from all the countries its available. So I've been looking to see if anyone has been reviewing it.