javascript 网络工作者抛出“未捕获的错误:DATA_CLONE_ERR:DOM 异常 25”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7506635/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
'Uncaught Error: DATA_CLONE_ERR: DOM Exception 25' thrown by web worker
提问by thatmiddleway
So I'm creating a web worker:
所以我正在创建一个网络工作者:
var arrayit = function(obj) {
return Array.prototype.slice.call(obj);
};
work = arrayit(images);
console.log(work);
//work = images.push.apply( images, array );
// Method : "load+scroll"
var worker = new Worker('jail_worker.js');
worker.postMessage(work)
worker.onmessage = function(event) {
console.log("Worker said:" + event.data);
};
Here's what images is:
以下是图像:
$.jail.initialStack = this;
// Store the selector into 'triggerEl' data for the images selected
this.data('triggerEl', (options.selector) ? $(options.selector) : $window);
var images = this;
I think my problem has something to do with this:
我认为我的问题与此有关:
http://dev.w3.org/html5/spec/Overview.html#safe-passing-of-structured-data
http://dev.w3.org/html5/spec/Overview.html#safe-passing-of-structured-data
How can I get around this? as you can see, I tried slicing the host object into a real array, but that didn't work.
我怎样才能解决这个问题?如您所见,我尝试将宿主对象切片成一个真正的数组,但这没有用。
Here's a link to the file I'm hacking on:
这是我正在破解的文件的链接:
https://github.com/jtmkrueger/JAIL
https://github.com/jtmkrueger/JAIL
UPDATE--------------------------------------------------
更新 - - - - - - - - - - - - - - - - - - - - - - - - - ——
This is what I had to do based on the accepted answer from @davin:
根据@davin 接受的答案,这是我必须做的:
var arrayit = function(obj) {
return Array.prototype.slice.call(obj);
};
imgArray = arrayit(images);
work = _.map(images, function(i){ return i.attributes[0].ownerElement.outerHTML; });
var worker = new Worker('jail_worker.js');
worker.postMessage(work)
worker.onmessage = function(event) {
console.log("Worker said:" + event.data);
};
NOTE: I used underscore.js to assure compatibility.
注意:我使用 underscore.js 来确保兼容性。
回答by davin
The original exception was most likely thrown because you tried passing a host object to the web worker (most likely a dom element). Your subsequent attempts don't throw the same error. Remember two key points: there isn't shared memory between the different threads, and the web workers can't manipulate the DOM.
最初的异常很可能是因为您尝试将主机对象传递给 Web 工作者(很可能是 dom 元素)而引发的。您随后的尝试不会引发相同的错误。记住两个关键点:不同线程之间没有共享内存,Web Worker 无法操作 DOM。
postMessage
supports passing structured data to threads, and will internally serialise(or in some other way copy the valueof the data recursively) the data. Serialising DOM elements often results in circular reference errors, so your best bet is to map
the object you want serialised and extract relevant data to be rebuilt in the web worker.
postMessage
支持将结构化数据传递给线程,并将在内部序列化(或以某种其他方式递归复制数据的值)数据。序列化 DOM 元素通常会导致循环引用错误,因此最好的办法是map
序列化对象并提取相关数据以在 Web Worker 中重建。
回答by Blackrabbit99
Uncaught DataCloneError: An object could not be cloned
was reproduced when tried save to indexeddb function as object's key. Need double recheck that saved object is serializable
Uncaught DataCloneError: An object could not be cloned
尝试保存到 indexeddb 函数作为对象的键时被复制。需要再次检查保存的对象是否可序列化