Problem with pausing when minimizing the game Godot 4.2

Pause does not work if you build the game in WEB

func _notification(what):
	match what:
		NOTIFICATION_APPLICATION_FOCUS_IN:
			toggle_pause(false)
		NOTIFICATION_APPLICATION_FOCUS_OUT:
			toggle_pause(true)
func toggle_pause(is_paused: bool):
	get_tree().paused = is_paused
	AudioServer.set_bus_mute(AudioServer.get_bus_index("Master"), is_paused)

Pause does work on WEB build.

Just tried:

get_tree().paused = true

And it worked.

The pause works, but I need it to be put after I minimise to another browser tab

Yep that works too.

I check if tab is visible with this:

JavaScriptBridge.eval("document.visibilityState") == "visible"

And used the same get_tree().paused = true. And it does works.
Just this node needs to have Process Mode = Always.

1 Like

Can you please show an example of how to use?

extends Node
class_name Pauser

func _ready() -> void:
	var timer: Timer = Timer.new()
	add_child(timer)
	timer.wait_time = 0.1
	timer.timeout.connect(_check_tab_visible)
	timer.start()

func _check_tab_visible() -> void:
	var is_visible = JavaScriptBridge.eval("document.visibilityState") == "visible"
	get_tree().paused = not is_visible
	JavaScriptBridge.eval("document.title = '" + str(get_tree().paused) + "';")

extends Control
class_name Main

@onready var label: Label = $Label
@onready var label_2: Label = $Label2

func _ready() -> void:
	var timer: Timer = Timer.new()
	timer.wait_time = 1.0;
	add_child(timer)
	timer.timeout.connect(func(): label.text = str(int(label.text) + 1.0))
	timer.start()

func _process(delta: float) -> void:
	var new_value: String = str(int(label_2.text) + 1.0)
	
	label_2.text = new_value


Thanks, it works! But i have another problem, if i minimise to another tab the game doesn’t pause, i think the game doesn’t work if i switch to another tab