Chrome: timeouts/interval suspended in background tabs?

后端 未结 6 1416
清酒与你
清酒与你 2020-11-22 02:47

I was testing the accuracy of setTimeout using this test. Now I noticed that (as expected) setTimeout is not very accurate but for most appliances

6条回答
  •  失恋的感觉
    2020-11-22 03:21

    Playing an ~empty sound forces the browser to retain the performance - I discovered it after reading this comment: How to make JavaScript run at normal speed in Chrome even when tab is not active?

    I need unlimited performance on-demand for a browser game that uses WebSockets, so I know from experience that using WebSockets doesn't ensure unlimited performance, but from tests, playing an audio file seems to ensure it

    Here's 2 empty audio loops I created for this purpose, you can use them freely, commercially: http://adventure.land/sounds/loops/empty_loop_for_js_performance.ogg http://adventure.land/sounds/loops/empty_loop_for_js_performance.wav

    (They include -58db noise, -60db doesn't work)

    I play them, on user-demand, with Howler.js: https://github.com/goldfire/howler.js

    function performance_trick()
    {
        if(sounds.empty) return sounds.empty.play();
        sounds.empty = new Howl({
            src: ['/sounds/loops/empty_loop_for_js_performance.ogg','/sounds/loops/empty_loop_for_js_performance.wav'],
            volume:0.5,
            autoplay: true, loop: true,
        });
    }
    

    It's sad that there is no built-in method to turn full javascript performance on/off by default, yet, crypto miners can hijack all your computing threads using Web Workers without any prompt :|

提交回复
热议问题