[SOLVED] Where to put visibilitychange-function

Hi @Thomas_Due_Nielsen,

Did you try adding the event to the document element? You can add this code in a regular script at the Playcanvas initialize() method (taken from the MDN JavaScript docs):

//startSimulation and pauseSimulation defined elsewhere
function handleVisibilityChange() {
  if (document.hidden) {
    pauseSimulation();
  } else  {
    startSimulation();
  }
}

document.addEventListener("visibilitychange", handleVisibilityChange, false);
1 Like