# \[SOLVED\] Continue Animation on tabs change also

**URL:** <https://forum.playcanvas.com/t/solved-continue-animation-on-tabs-change-also/23500>\
**Category:** Help & Support\
**Created:** [December 23, 2021, 6:53am UTC](https://forum.playcanvas.com/t/solved-continue-animation-on-tabs-change-also/23500 "2021-12-23T06:53:37Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Harsh\_Zota](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/harsh_zota/32/18771_2.png) [@Harsh\_Zota](https://forum.playcanvas.com/u/Harsh_Zota)\
**Post date:** [December 23, 2021, 6:53am UTC](https://forum.playcanvas.com/t/solved-continue-animation-on-tabs-change-also/23500/1 "2021-12-23T06:53:37Z")

</div>

Hi I am making a third person game , so I need to keep it running like a video , but I observed that my animations(more like my tween movements with which I move the entity) stop when I change tabs , is there any way I can keep it running in the background like even when a person switches tabs or windows?

---

<div class="post-metadata">

**Author:** ![Leonidas](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/leonidas/32/6495_2.png) [@Leonidas](https://forum.playcanvas.com/u/Leonidas)\
**Post date:** [December 23, 2021, 8:51am UTC](https://forum.playcanvas.com/t/solved-continue-animation-on-tabs-change-also/23500/2 "2021-12-23T08:51:53Z")

</div>

Hi @Harsh_Zota,

I think what has been said in this thread holds true for what you are asking here:

> [@Keep my application running in background](https://forum.playcanvas.com/t/keep-my-application-running-in-background/23394):
>
> Is it possible to keep my game/application keep on running in background even when the user switches tabs

To sum up, you can’t do that without messing with the PlayCanvas engine. PlayCanvas will update the component, rendering, animation etc systems on each browser animation frame update. Which will pause as soon as the tab becomes inactive.

You could potentially override the following pc.Application class to update on a `setInterval` instead of `requestAnimationFrame`, as we discussed on the other thread, but beware potentially that will not work as expected depending on how the browser behaves.

> <https://github.com/playcanvas/engine/blob/415bd6368a96656161a503526950413dedc87c7e/src/framework/application.js#L2054>

---

<div class="post-metadata">

**Author:** ![Harsh\_Zota](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/harsh_zota/32/18771_2.png) [@Harsh\_Zota](https://forum.playcanvas.com/u/Harsh_Zota)\
**Post date:** [December 23, 2021, 8:55am UTC](https://forum.playcanvas.com/t/solved-continue-animation-on-tabs-change-also/23500/3 "2021-12-23T08:55:11Z")

</div>

so should I append this pc.Application script to my root?

---

<div class="post-metadata">

**Author:** ![Leonidas](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/leonidas/32/6495_2.png) [@Leonidas](https://forum.playcanvas.com/u/Leonidas)\
**Post date:** [December 23, 2021, 8:58am UTC](https://forum.playcanvas.com/t/solved-continue-animation-on-tabs-change-also/23500/4 "2021-12-23T08:58:08Z")

</div>

No, it’s not that simple, you will have to either modify the PlayCanvas engine and build from source, or attempt to patch the pc.Application class.

Both are advanced actions and I wouldn’t go that way normally, since it won’t fully work (the browser throttles setTimeout and setInterval).

Why would you like to keep animations running when the tab is inactive?

---

<div class="post-metadata">

**Author:** ![Harsh\_Zota](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/harsh_zota/32/18771_2.png) [@Harsh\_Zota](https://forum.playcanvas.com/u/Harsh_Zota)\
**Post date:** [December 23, 2021, 9:14am UTC](https://forum.playcanvas.com/t/solved-continue-animation-on-tabs-change-also/23500/5 "2021-12-23T09:14:11Z")

</div>

As I mentioned above, I am making a third person game, so irrespective if a player is there or not , or even after him joining the game , if he switches tabs or minimizes them I need the game to keep on running , I am working on card games like poker , so as per values from Api’s I am triggering the animation.  
Now what is happening is that  
Let us say in poker we are dealing the player hand cards, player has joined the game when no cards have been dealt, but after 1 card of player 1 is dealt he switches tabs or minimizes them and returns when the second card of third player(assume there are 3 players) is being dealt , so what happens is the animation of the second card of third player only, it skips the animation of both card of second player and first card of first player, it does not show the cards only ,but when I trigger animation to exit cards , it exits all the cards from where it is supposed to.

So to this problem I though what if I could run animations in background, if I can do that I will not face any such problems.  
If there is any alternate way I can see all these animations and cards , please suggest.

Also I have added you to my project so you can see it. If you go through my project and after launching if you switch tabs you will see what I am saying exactly.  
Thanks  
Hoping to find a solution

---

<div class="post-metadata">

**Author:** ![Leonidas](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/leonidas/32/6495_2.png) [@Leonidas](https://forum.playcanvas.com/u/Leonidas)\
**Post date:** [December 23, 2021, 9:26am UTC](https://forum.playcanvas.com/t/solved-continue-animation-on-tabs-change-also/23500/6 "2021-12-23T09:26:23Z")

</div>

I see, but your main problem is the way browsers handle inactive tabs. They can’t guarantee that your animations will continue playing at the same speed (if playing at all) even if you use the hack I am talking about (setInterval).

I think the proper solution here is to handle your game logic as states, so each time the tab becomes active again you sync the current state of your game and fast forward any animations that were required to be played. The moment the tab becomes active request the latest game state from your server and update the local client to that.

---

<div class="post-metadata">

**Author:** ![Harsh\_Zota](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/harsh_zota/32/18771_2.png) [@Harsh\_Zota](https://forum.playcanvas.com/u/Harsh_Zota)\
**Post date:** [December 23, 2021, 9:34am UTC](https://forum.playcanvas.com/t/solved-continue-animation-on-tabs-change-also/23500/7 "2021-12-23T09:34:17Z")

</div>

> [@Leonidas](#):
>
> local

How do I check if my tabs have become active or inactive?

---

<div class="post-metadata">

**Author:** ![Leonidas](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/leonidas/32/6495_2.png) [@Leonidas](https://forum.playcanvas.com/u/Leonidas)\
**Post date:** [December 23, 2021, 9:34am UTC](https://forum.playcanvas.com/t/solved-continue-animation-on-tabs-change-also/23500/8 "2021-12-23T09:34:47Z")

</div>

You can use the browser page visibility API:

> **[Page Visibility API - Web APIs | MDN](https://developer.mozilla.org/en-US/docs/Web/API/Page_Visibility_API)**
>
> The Page Visibility API provides events you can watch for to know when a document becomes visible or hidden, as well as features to look at the current visibility state of the page.

---

<div class="post-metadata">

**Author:** ![Harsh\_Zota](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/harsh_zota/32/18771_2.png) [@Harsh\_Zota](https://forum.playcanvas.com/u/Harsh_Zota)\
**Post date:** [December 23, 2021, 10:04am UTC](https://forum.playcanvas.com/t/solved-continue-animation-on-tabs-change-also/23500/9 "2021-12-23T10:04:43Z")

</div>

This seems to be helpful but it seems only to work on chrome and not firefox

---

<div class="post-metadata">

**Author:** ![yaustar](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/yaustar/32/2268_2.png) [@yaustar](https://forum.playcanvas.com/u/yaustar)\
**Post date:** [December 23, 2021, 10:14am UTC](https://forum.playcanvas.com/t/solved-continue-animation-on-tabs-change-also/23500/10 "2021-12-23T10:14:04Z")

</div>

![image](https://forum-files-playcanvas-com.s3.dualstack.eu-west-1.amazonaws.com/original/3X/2/7/27798fc578e3a618a3f179a28c2a0a90b1b43ed8.png)

Compatibility says it should work on Firefox

---

<div class="post-metadata">

**Author:** ![Harsh\_Zota](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/harsh_zota/32/18771_2.png) [@Harsh\_Zota](https://forum.playcanvas.com/u/Harsh_Zota)\
**Post date:** [December 23, 2021, 11:28am UTC](https://forum.playcanvas.com/t/solved-continue-animation-on-tabs-change-also/23500/11 "2021-12-23T11:28:42Z")

</div>

Yes even I saw that but I use firefox only and It did not seem to work.  
Let’s see I shall implement this in my project

---

<div class="post-metadata">

**Author:** ![Albertos](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/albertos/32/19749_2.png) [@Albertos](https://forum.playcanvas.com/u/Albertos)\
**Post date:** [December 23, 2021, 11:59am UTC](https://forum.playcanvas.com/t/solved-continue-animation-on-tabs-change-also/23500/12 "2021-12-23T11:59:07Z")

</div>

Hi @Harsh_Zota! I’ve pointed this out before, but there is a difference between a _tween_ and _animation_. You can use a _tween_ to create an animation, but it’s still a _tween_. Your questions and the answers to them can therefore become confusing and incorrect.

> [@Harsh\_Zota](#):
>
> How do I check if my tabs have become active or inactive?

Is `window.onblur` and `window.onfocus` an option for this?

---

<div class="post-metadata">

**Author:** ![Harsh\_Zota](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/harsh_zota/32/18771_2.png) [@Harsh\_Zota](https://forum.playcanvas.com/u/Harsh_Zota)\
**Post date:** [January 4, 2022, 9:43am UTC](https://forum.playcanvas.com/t/solved-continue-animation-on-tabs-change-also/23500/13 "2022-01-04T09:43:43Z")

</div>

> [@Albertos](#):
>
> Is `window.onblur` a

I resolved my problems with your help, instead of detecting window blur or focused , I never paused my animation hence on tab change or minimizing , my animation never stops
