简体   繁体   English

在最小化上暂停Javascript动画

[英]Pausing Javascript animation on minimize

I am using the Chart.js library for my animated graphs on the website. 我将Chart.js库用于网站上的动画图。 From reading other discussions on the subject, browsers treat SetInterval() code differently when a tab is inactive or minimized. 通过阅读有关该主题的其他讨论,浏览器在选项卡处于非活动状态或最小化时会以不同的方式对待SetInterval()代码。

I noticed one suggestion stating that stopping the animation and restarting it when the tab regains focus should correct the issue. 我注意到一个建议,指出当标签重新获得焦点时停止动画并重新启动它应该可以解决此问题。 I am not sure how to do this nor can I find any code. 我不确定如何执行此操作,也无法找到任何代码。 The library has "animation: true" where I believe this can be used to stop/start it, but not sure how to implement it. 该库具有"animation: true" ,我相信它可以用来停止/启动它,但不确定如何实现它。

setInterval(function()
{
    $.ajax({
        type: "POST",
        url: "<?= $data['baseUrl']; ?>" +  "graphs.json",
        data: { 'action': 'getServerStats' },
        dataType: 'json',
        success: function(data)
        {
            ram = data['result']['RAMUsageMB'];

            // Add two random numbers for each dataset
            ramGraph.addData([ram], '');

            // Remove the first point so we dont just add values forever
            ramGraph.removeData();
        }
    });
}, 1000);
document.addEventListener("visibilitychange", function() {
  animation = ! document.hidden;
}, false);

https://developer.mozilla.org/en-US/docs/Web/Guide/User_experience/Using_the_Page_Visibility_API Works in IE 10+, Firefox 18+ and Chrome 33+ (according to source). https://developer.mozilla.org/zh-CN/docs/Web/Guide/User_experience/Using_the_Page_Visibility_API在IE 10以上版本,Firefox 18以上版本和Chrome 33以上版本中有效(根据来源)。 You'll have to change the animation = !document.hidden; 您必须更改animation = !document.hidden; yourself as you were a bit vague on this topic, but this should give you an idea. 自己,因为您对此主题有点含糊,但这应该可以给您一个想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM