Switch Tab on Web

i’m creating a counter when switching tabs back i’m not getting it correct
i use EVENT_SHOW and EVENT_HIDE
cocos creator ver 3.8
please help me!

From the code docs

    /**
     * @en Event triggered when game hide to background.<br>
     * Please note that this event is not 100% guaranteed to be fired on Web platform,<br>
     * on native platforms, it corresponds to enter background event, os status bar or notification center may not trigger this event.
     * @example
     * ```ts
     * import { game } from 'cc';
     * game.on(Game.EVENT_HIDE, function () {
     *
     * });
     * ```
     */
    static readonly EVENT_HIDE = "game_on_hide";

Your best option here is to use the Game class events EVENT_PAUSE and EVENT_RESUME

How are you creating the counter? Using any timer function like set interval or cocos inbuilt timer function?

When I switch tabs for a long time, my counter goes negative

Check if you’re using the right events like EVENT_PAUSE and EVENT_RESUME for your counter. If it goes negative, look at how you update it when switching tabs.