I want stop the sounds from playing when the webpage is hidden

I tried to use document.hidden but I doesn’t seem to work .

Hi,

It works just fine in the p5 web editor :

function setup() {
  createCanvas(400, 400);
}

function draw() {
  background(220);
  
  if (document.hidden) console.log("hidden");
}

It’s printing hidden when I switch tab or if I hide the window (I am on Firefox :yum: ).

I doesn’t seem to work on chrome.

Are you sure the window is not visible? Can you try to open a new tab and switch to it?

Also on which operating system are you running on?

Yea I tried printing in console to see if it was visible at one point but doesn’t seem to work. I’m using windows

Ok weird :wink:

To test it :

  • Close your browser and all the tabs

  • Open it

  • Go to the p5 web editor and copy paste the code above

  • Press the run button (Ctrl+Enter)

  • Minimize your window or open a new tab, make sure the window is hidden

  • Inspect the console and also press F12 to see the inspector → does it show “hidden”?

From the browser compatibility section, it says that it’s supported by Chrome released in 2014…

function setup() {
  createCanvas(400, 400);
}

function draw() {
  background(220);
  if (document.hidden) console.log("hidden");
  else
    print('visible');
}

but just prints visible
I’m using the newestversion of chrome

Hum, the code is not correct :grinning:

Also not that you can format your code by using the </> button when editing a message or use backticks around your code : ``` code ``` → code