# engine.getDeltaTime huge when window returns focus

**URL:** <https://forum.babylonjs.com/t/engine-getdeltatime-huge-when-window-returns-focus/50652>\
**Category:** Questions\
**Created:** [May 18, 2024, 12:54pm UTC](https://forum.babylonjs.com/t/engine-getdeltatime-huge-when-window-returns-focus/50652 "2024-05-18T12:54:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ben\_palmer](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/ben_palmer/32/39804_2.png) [@ben\_palmer](https://forum.babylonjs.com/u/ben_palmer)\
**Post date:** [May 18, 2024, 12:54pm UTC](https://forum.babylonjs.com/t/engine-getdeltatime-huge-when-window-returns-focus/50652/1 "2024-05-18T12:54:30Z")

</div>

I am using engine.getDeltaTime ensure my simulation performs at a constant rate, but have found that if the tab loses focus, when the user refocuses a potentially huge getDeltaTime is returned.

My solution is below, just wondered if there was a better way.  
let hasFocus = true;  
let firstOnFocus=false;

```
engine.runRenderLoop(function () 

    if (hasFocus){
      if (firstOnFocus){
        mainLoop.updateTime(0); // trash the DeltaTime if just returned focus
        firstOnFocus=false;
      } else {
        mainLoop.updateTime(engine.getDeltaTime());
      }
      scene.render();
    }  
});

```

document.addEventListener(“visibilitychange”, function() {  
if (document.visibilityState === ‘visible’) {  
hasFocus=true;  
firstOnFocus=true;  
} else {  
hasFocus=false;  
firstOnFocus=false;  
}  
});

---

<div class="post-metadata">

**Author:** ![labris](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/labris/32/12576_2.png) [@labris](https://forum.babylonjs.com/u/labris)\
**Post date:** [May 18, 2024, 4:28pm UTC](https://forum.babylonjs.com/t/engine-getdeltatime-huge-when-window-returns-focus/50652/2 "2024-05-18T16:28:50Z")

</div>

Hello and welcome!

The usage of Page Visibility API to avoid large time delta values is quite common and correct.  
Here is the example from Yuka library - [yuka/src/core/Time.js at master · Mugen87/yuka · GitHub](https://github.com/Mugen87/yuka/blob/master/src/core/Time.js#L24)  
One may check Yuka+Babylon examples here - [Yuka | Examples with Babylon.js](https://yuka.babylonpress.org/examples/)

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [May 20, 2024, 4:26pm UTC](https://forum.babylonjs.com/t/engine-getdeltatime-huge-when-window-returns-focus/50652/3 "2024-05-20T16:26:38Z")

</div>

This raises an interesting question to know if time should continue when rendering is paused which honestly probably depends on the use case.

cc @Deltakosh ?

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [May 20, 2024, 4:36pm UTC](https://forum.babylonjs.com/t/engine-getdeltatime-huge-when-window-returns-focus/50652/4 "2024-05-20T16:36:14Z")

</div>

We do have an option for it 🙂

you can set `engine.renderEvenInBackground = false`
