when XMLHttpRequest gets deallocated?

Viewed 38

Let's see the code (that's entire html page)

<script>
function ajax_async(method, url, body, cb) {
  var r = new XMLHttpRequest();
  r.open(method, url, true);
  r.onreadystatechange = cb;
  r.send(body);
}

ajax_async('GET', '/sleep.php', null, function() {
  if(this.readyState==4) alert('123');
});
</script>

sleep.php just sleeps 10 sec and returns

Obviously, XMLHttpRequest object uses some memory and should be deallocated after use if we want long-living application page. As I know, javascript uses garbage collector to free unused items in memory, and "unused" means "can't be accessed".

In the code above, the variable r is local and can't be accessed outside ajax_async() function. But the request continues to work and the callback function called after 10 sec, so the XMLHttpRequest object still exists and didn't deallocated. Why? Will it be deallocated after readyState becomes 4? If no, when it will be? If yes, why, is readyState==4 means "deallocatable" and others values "deallocatable", is this specified somewhere?

0 Answers
Related