Hi all !

I'm facing this issue: I made an HTML5 game, with sounds inside, related to events.
This works fine, audio is played in Chrome/Firefox etc.... when the game has the focus (the tab is selected). If I go to another tab in my browser, the sound is not played. Is there a way to play a sound in an HTML5 event if the current tab does not have the focus ?
Thanks.

Edit: I'm on Godot 3.5

On HTML5 in general, the app is put into a sort of sleep state when not the current tab. I don't think you can override this.

OMG 🙁 It's a roadblock for a game based on a timer (like Scrabble or Chest) for which you need to know when the turn ends.....

Base the timer on UNIX time (the CPU clock) not a counter/timer.

On Godot 4 you can use the Time class:
https://docs.godotengine.org/en/stable/classes/class_time.html

So something like this:

var time_last_move = Time.get_ticks_msec() # save this when you make a move
var time_elapsed = Time.get_ticks_msec() - time_last_move
if time_elapsed > 5000:
    print("5 seconds has passed")

Should work in Godot 3.5 the same.

Sorry but I can't understand: It won't change anything since when the tab is not focused, Godot "process" function is not executed. So the elapsed time won't be calculated.
Thanks anyway.

It will be once you come back to the tab. Nothing is going to update if the tab is hidden.

Ok, thanks. This is my current behavior (already coded with a mix of JS inside gdscript) in order to update the timer through JS + _process function if the tab is not selected. But here what I expect is that the sound is played even if the browser tab in which the game is executed IS NOT selected.

Thanks.

I asked ChatGPT.

Me

Can an hidden web browser tab be forced to remain active?

ChatGPT

As an AI language model, I don't have direct access to real-time information about software updates or browser features beyond my September 2021 knowledge cutoff. However, as of my last update, it is not possible for a hidden web browser tab to remain active indefinitely.

Web browsers are designed to optimize system resources and prioritize active tabs that are currently being used by the user. When a tab is hidden or in the background, the browser may reduce its resource allocation, including processing power and memory usage, to improve overall system performance.

There are various techniques used by browsers to suspend or throttle the activities of inactive tabs, such as reducing JavaScript execution, limiting network requests, and pausing animations. These measures help conserve system resources and extend battery life on devices.

While some browsers offer features like "tab freezing" or "tab discarding" to manage system resources better, these features are generally aimed at reducing resource consumption rather than keeping hidden tabs consistently active.

It's important to note that browser behavior can change over time as new updates and features are introduced. Therefore, it's advisable to consult the documentation or official sources for your specific browser version to get the most accurate and up-to-date information on its capabilities.

Me

Now, write a very short poem about this in the style of Shakespeare.

ChatGPT

In shadows deep, where browsers dwell,
A hidden tab, in slumber fell.
Its presence masked, by idle guise,
Concealing secrets from prying eyes.

Yet fate, unkind, does intervene,
As sleep befalls this hidden screen.
No longer shall its spark ignite,
Lost in the depths of digital night.

For browsers fair, with cunning guise,
Assign their resources, to arise
Those tabs, in focus, they favor well,
While hidden ones, in slumber dwell.

Oh, hidden tab, thou art deceived,
By browsers' will, thou art bereaved.
No force, nor plea, can keep thee woke,
In silent slumber, thou art cloak'd.

So rest, dear tab, in realms unknown,
In Shakespearean verse, thy fate is shown.
No power can defy thy dormant state,
In shadows hidden, thou shalt await.

Me

I asked for a very short poem. That was too long.

ChatGPT

Apologies for the length,
Allow me to amend my poetic strength.

In hidden depths, a tab doth lie,
Forced to slumber, no light in the sky.

Alas! No power keeps it awake,
In hidden realm, its fate it must take.