“未捕获错误:DATA_CLONE_ERR:DOM 异常 25”由网络工作者抛出
所以我正在创建一个网络工作者:
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);
};
这就是图像:
$.jail.initialStack = this;
// Store the selector into 'triggerEl' data for the images selected
this.data('triggerEl', (options.selector) ? $(options.selector) : $window);
var images = this;
我认为我的问题与此有关:
http://dev.w3.org/html5/spec/Overview.html#safe-passing-of-structed-data
我该如何解决这个问题?如您所见,我尝试将主机对象切片为真实数组,但这不起作用。
这是我正在破解的文件的链接:
https://github.com/jtmkrueger/JAIL
更新---- ----------------------------------------------------------
这就是我的必须根据 @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);
};
注意:我使用 underscore.js 来确保兼容性。
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
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
UPDATE--------------------------------------------------
This is what I had to do based on the accepted answer from @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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最初的异常很可能是因为您尝试将主机对象传递给 Web Worker(很可能是 dom 元素)而引发的。您后续的尝试不会引发相同的错误。请记住两个关键点:不同线程之间不存在共享内存,并且 Web Worker 无法操作 DOM。
postMessage
支持将结构化数据传递给线程,并且将 在内部序列化(或以其他方式递归复制数据的值)数据。序列化 DOM 元素通常会导致循环引用错误,因此最好的选择是映射要序列化的对象并提取要在 Web Worker 中重建的相关数据。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.
postMessage
supports passing structured data to threads, and will internally serialise (or in some other way copy the value of the data recursively) the data. Serialising DOM elements often results in circular reference errors, so your best bet is tomap
the object you want serialised and extract relevant data to be rebuilt in the web worker.当尝试将对象的键保存到 indexeddb 函数时,会重现
未捕获的 DataCloneError:无法克隆对象
。需要仔细检查保存的对象是否可序列化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