Javascript - 如何将jQuery对象(表)传递给Web worker? [英] Javascript - How can I pass a jQuery object (table) to a web worker?

查看:913
本文介绍了Javascript - 如何将jQuery对象(表)传递给Web worker?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含不同列和行的HTML表。该表可以由用户内联编辑。当用户编辑表时,我会在表的行上计算一些总和。

I have an HTML table with different columns and rows. The table can be edited inline by a user. When a user edits the table, I calculate some sums on the rows of table.

计算总和的函数在主脚本中并花了很多时间制作浏览器没有响应。为了解决这个性能问题,我在JavaScript中创建了一个web worker来计算表中的总和。

The function that calculates the sums was in the main script and took a lot of time making the browser unresponsive. To solve this performance issue, I have created a web worker in JavaScript to calculate the sums on the table.

问题是Web worker无法访问DOM。我正在寻找一种方法将jQuery对象表传递给Web worker。

The problem is that the web worker cannot access the DOM. I'm looking for a way to pass a jQuery object table to the web worker.

如果我尝试传递jQuery对象,我收到一个错误:

If I try to pass the jQuery object I receive an error:


未捕获DataCloneError:无法在'Worker'上执行'postMessage':无法克隆对象。

Uncaught DataCloneError: Failed to execute 'postMessage' on 'Worker': An object could not be cloned.

如何将表传递给Web Worker?

How I can pass the table to the Web Worker?

谢谢

总和需要很长时间,因为该表有很多行并计算不同的总和(总数,小计等)。 sum的值存储在表中(因此worker需要访问表来执行计算)。

The sum take a long time because the table has a lot of rows and calculate different sums (total, subtotal,etc). The values to sum are stored in table (so worker need to access to table to perform the calculation).

我的想法是将DOM对象传递给worker以计算总和。
计算完成后,worker将总和返回给主线程以更新DOM中的值。

My idea is to pass DOM object to worker to calculate sums. After calculation, worker return the sums to main thread in order to update values in the DOM.

推荐答案

简而言之你不能将DOM元素传递给worker。只有数据(通常是字符串)才能传递给Web工作者而不是对象。您应该从表中提取数据,将其包装在对象中,将对象转换为json,将json发送给worker,处理它并将一些json返回给主线程。

In short you cannot pass a DOM element to a worker. Only data, typically as strings, can be passed to webworkers not objects. You should extract your data from your table, wrap it up in an object, turn your object into json, send the json to the worker, process it and return some json back to the main thread.

这篇关于Javascript - 如何将jQuery对象(表)传递给Web worker?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆