Ask coding questions

← Back to all posts
JavaScript FinalizationRegistry not working right
xxpertHacker (768)

It seems that my FinalizationRegistry isn't being called at all upon garbage collection of my DOM paragraph element.

It clearly gets GCed at some point, but the code should say "bye" after it gets GCed, yet it appears to never be called at all.

(The Counter class is stolen and edited from MDN's article on weak references.)

Right now it counts every second until GC, then it says that the "element is gone," but it doesn't say "bye," which is what the Registry is setup to do.

Comments
hotnewtop
Whacko (296)

ooooh I see. Your files are not linked together. This is how your code should be:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>Title</title>
    <link href="style.css" rel="stylesheet" type="text/css" />
  </head>
  <body>
    <meta http-equiv="refresh" content="0; url=index.xhtml">
  </body>
</html>

Although the .mjs files might be a problem. Can they work as .js files?

xxpertHacker (768)

@JavaMaster1 My files are definitely linked together, you shouldn't really put a <meta> tag in the <body>, and it even creates the counter going down until GC.

Whacko (296)

@xxpertHacker OH! You forgot a " in front of the url:
What you wrote:

<meta http-equiv="refresh" content="0; url=index.xhtml">

Correct:

<meta http-equiv="refresh" content="0; url="index.xhtml">
xxpertHacker (768)

@JavaMaster1 I have no clue what you're trying to say,
The HTML page works as intended.
This is what I'm talking about:

Whacko (296)

@xxpertHacker Copy the correct code and replace your code. Tell me if it works.

xxpertHacker (768)

@JavaMaster1 It won't, because what you gave is just plain wrong.

Whacko (296)

@xxpertHacker Really? I thought you needed the two ""s in between each of the attributes.

xxpertHacker (768)

@JavaMaster1 Look closely, there are both, the beginning and ending quote. Does it not redirect you to the proper page?

Whacko (296)

@xxpertHacker oooh yeah. Hmmmm... I don't know what the heck mjs is, I only know JavaScript. maybe run a node.js and connect all the files using JavaScript?

xxpertHacker (768)

@JavaMaster1 MJS is merely ES5 Module JavaScript. It's almost 6 years old, and supported by every major browser. I'm not about to use Node for a webpage :/ Node is terrible.

The page works in its current state:
https://ref.xxperthacker.repl.co/index.xhtml
Are you unable to see it at all?

Whacko (296)

@xxpertHacker Wait-so what you're trying to do... is a countdown?

xxpertHacker (768)

@JavaMaster1 No, what I am doing is a countdown, until the element is GCed. Notice that the countdown is non-deterministic. Look at my code, read it yourself. I feel like you have absolutely no clue what my code does, nor what the web page does, at all.

xxpertHacker (768)

@JavaMaster1 I'm trying to run the FinalizationRegistry, which appears to not be working at all.

Whacko (296)

@xxpertHacker Whatever all these mixed-up words are, I don't know. But if you need a countdown, I can just give you a script.

xxpertHacker (768)

@JavaMaster1 I am more than capable of making a braindead simple countdown. I am not trying to make a countdown, and if I were, I would already be done, since it already counts down.
If I might ask, do you comprehend what the question at the top of this post even says?

Whacko (296)

@xxpertHacker Hmmmm... well, I don't know any of this yet, just started JavaScript this summer, so I can't help you then.

CodeLongAndPros (1564)

@Whacko (sorry for necropost)
If you don't know how to help them, that's fine, say so. Don't try and bluff your way though it and add meaningless fluff.

Oh, I don't know about that, sorry for the mixup

Is totally fine, no shame.