Hi all,

my question is related to the HTML5 target. When switching from one tab to another in the same browser, or reducing the browser, timers and processes are stopped. Is there a way to receive a signal when the tab gains the focus ?

Hmm, that's an interesting question.

I haven't looked into this but maybe you might be able to pause the game via the engine class in your games HTML shell page: https://docs.godotengine.org/en/stable/tutorials/platform/customizing_html5_shell.html#doc-customizing-html5-shell

https://www.codingwithjesse.com/blog/detect-browser-window-focus/

Perhaps @Calinou might be able to offer a more clear answer, or perhaps this is something being considered/worked on for a future version if the above suggestion doesn't pan out. I don't do anything with godot targeting HTML5 myself so I'm not really sure.

Hello, thanks a lot for the help. Regarding that, I tried things in the version of Godot 3.4 that allows to access to Javascript objects directly. But nothing works. By analogy to the JS code:

<script>
document.addEventListener("visibilitychange", myFunction);

function myFunction() {
	console.log(document.hidden);
}
</script> 

I tried in Godot, in my main script:


    extends Node
    
    var callback = JavaScript.create_callback(self, "_on_focus_in")
    
    func _on_focus_in():
       print("FOCUSSSSSS IN")
    
    func _ready():
    	JavaScript.get_interface("document").addEventListener("visibilitychange", callback)

Do you have any clue if we can use addEventListener in Godot thru a JavascriptObject ?

Many thanks.

OMG ! I couldn't figure out this possibility. Thanks you so much for the help, you saved me !!! So, the solution is:

func _on_focus_in(event):
       print("FOCUSSSSSS IN")

And how to put this topic has solved ?

I changed it to a Q&A and marked it as solved for you. Also, this is very good to know. Thanks.

a year later