Can I trigger JavaScript's garbage collection?

后端 未结 9 2250
不知归路
不知归路 2021-01-31 07:17

I want to trigger JavaScript garbage collection. Is it possible? Why would I want to, or not want to, do this?

9条回答
  •  独厮守ぢ
    2021-01-31 07:55

    I was reading Trevor Prime's answer and it gave me a chuckle but then I realized he was on to something.

    1. Reloading the page does 'garbage-collect'.
    2. location.reload() or alternatives to refresh page.
    3. JSON.parse/stringify and localStorage.getItem/setItem for persistence of needed data.
    4. iframes as reloading pages for user experience.

    All you need to do is run your code in an iframe and refresh the iframe page while saving useful information into localStorage. You'll have to make sure the iframe is on the same domain as main page to access its DOM.

    You could do it without an iframe but user experience will no doubt suffer as the page will be visibly resetting.

提交回复
热议问题