Is this a good way to garbage collect
function getFile() {
var xhr = new XMLHttpRequest();
xhr.onload = function() {
//do stuff
delete(xhr)
};
}
and then call getFile()
a couple of times
(I've edited it to use a function)
How would I get rid of xhr
when it's done. I'm just a bit confused on whether its ok to remove xhr from within a function of itself
You should use scoping to do the trick for you. In JavaScript scope is defined by the function statement. Variables that are defined within a function will be deleted automatically when the function goes out of scope (and you did not use the variable in a closure)
So in your case:
function scope() {
var xhr = new XMLHttpRequest();
xhr.onload = function() {
//do stuff
};
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.