假设我们启动一个Worker并从blob创建一个对象URL:
//running in page window
var worker = new Worker(workerScriptUrl);
//running in worker
var u = rootScope.URL || rootScope.webkitURL;
var objUrl = u.createObjectURL(blob);
...现在(因为我们很懒)我们永远不会打电话:
u.revokeObjectURL(objUrl);
而是在启动worker的代码中,我们只需调用:
worker.terminate();
这会终止对象网址,还是会在原始网页窗口的持续时间内保留?
答案 0 :(得分:1)
好的,在没有答案的情况下,我决定自己测试一下:
var u = window.URL || window.webkitURL;
var f='('+
function(rootScope){
var u = rootScope.URL || rootScope.webkitURL;
var blob=new Blob([ "hello world!" ], { type: 'text/plain' } );
var blobURL = u.createObjectURL( blob) ;
rootScope.postMessage(blobURL);
}.toString()+
')(this)';
console.log(f);
// Build a worker from an anonymous function body
var blobURL = u.createObjectURL( new Blob([ f ],
{ type: 'application/javascript' } ) );
var worker = new Worker( blobURL );
worker.onmessage=function(e){
worker.terminate();
setTimeout(function(){
var xhr = new XMLHttpRequest();
xhr.open('GET', e.data, false);
xhr.send(null);
console.log( xhr.responseText ); //prints "hello world!"
},5000);
};
// Won't be needing this anymore
u.revokeObjectURL( blobURL );
可以看出,即使我们终止网络工作者五秒钟,我们仍然可以读取它创建的对象网址。