M
Mike Harrison
Hi, I've got a question regarding garbage collection of image objects
that are assigned to a local variable.
Is it safe to write code like below? This code assigns a new image to
a local variable and sets an onload handler, which gets called after
the variable has gone out of scope.
The handler always seems to get called even though there are no
remaining references to the object, so I wondered at what point the
image gets marked for garbage collection?
Is there a chance it could be deleted before the load handler is
called?
<script type="text/javascript">
function ImageLoaded(e)
{
alert ("Image loaded");
}
function LoadImage()
{
var img = document.createElement ("img");
img.addEventListener ("load", ImageLoaded, false);
img.src = "image.jpg";
}
LoadImage();
</script>
that are assigned to a local variable.
Is it safe to write code like below? This code assigns a new image to
a local variable and sets an onload handler, which gets called after
the variable has gone out of scope.
The handler always seems to get called even though there are no
remaining references to the object, so I wondered at what point the
image gets marked for garbage collection?
Is there a chance it could be deleted before the load handler is
called?
<script type="text/javascript">
function ImageLoaded(e)
{
alert ("Image loaded");
}
function LoadImage()
{
var img = document.createElement ("img");
img.addEventListener ("load", ImageLoaded, false);
img.src = "image.jpg";
}
LoadImage();
</script>