2

Expected behaviour: When i am on a page for x amount of time, once that time expires when i press back on the window i am redirected to a custom page of my choice, in this case google.

My code works however i noticed in the console i am getting a log that says "[Violation] 'setTimeout' handler took XXms"

Why does this happen? Is there another way to write what i am trying to achieve that is more performant perhaps using new Date() ?

        let redirect = false;

        const ref = setTimeout(() => {
            redirect = true;
            console.log('history');
        }, 5000);

        addEventListener('popstate', (event) => {
            if (redirect) {
                clearTimeout(ref);
                event.preventDefault();

                window.location.href = "https://www.google.com/"
            }
        });
mariann
  • 21
  • 4
  • it actually says `XXms`? - that's not very useful – Bravo Apr 20 '22 at 01:08
  • that was to demonstrate a value, should've probably written it clearer but it does display is correctly eg: 82ms – mariann Apr 20 '22 at 01:09
  • oh ... look ... https://stackoverflow.com/questions/42218699/chrome-violation-violation-handler-took-83ms-of-runtime - but thats 83ms ... I wonder if it's relevant for 82ms – Bravo Apr 20 '22 at 01:10
  • oh i see, hmm it's more so a warning. I wonder if there is a way to make it performant overall. Well there was this https://www.measurethat.net/Benchmarks/Show/14251/0/datenow-vs-settimeout#latest_results_block – mariann Apr 20 '22 at 01:13
  • Your handler couldn't take so long, so this warning is about a different one. – wOxxOm Apr 20 '22 at 05:54
  • what if i wanted it to go on for x hours ? Am i better off implementing it another way ? – mariann Apr 20 '22 at 23:28

0 Answers0