使用 jQuery.data() 丢失数据
我正在开发一个实用程序 Web 应用程序,以帮助操作一些特定于域的 XML 数据。
流程如下:
- 加载 XML 文件
- 使用浏览器的本机 XML 对象(不是 jQuery!)解析 XML 文件并转换为 JavaScript 对象。
- 使用 $(document).data() 存储结果对象
- 迭代对象并提取附加信息,将其存储在另一个 $(document).data() 槽中
window.setTimeout()
to split the work up into chunks.这是函数:
function explodeDataStep(index, max) {
var data = $(document).data('data');
var lists = $(document).data('lists');
$debug('explodeDataStep', index, $(document).data('data'), $.data(document));
var count = 0;
for (index; index < data.vehicles.length; index++) {
var vehicle = data.vehicles[index];
if ($.inArray(vehicle.make, lists.make) < 0) lists.make.push(vehicle.make);
if ($.grep(lists.model, function(v) { return v.make == vehicle.make && v.model == vehicle.model; }).length == 0) lists.model.push({ make: vehicle.make, model: vehicle.model });
if ($.inArray(vehicle.module, lists.module) < 0) lists.module.push(vehicle.module);
if ($.inArray(vehicle.doorlock, lists.doorlock) < 0) lists.doorlock.push(vehicle.doorlock);
if ($.inArray(vehicle.doorlockCombo, lists.doorlockCombo) < 0) lists.doorlockCombo.push(vehicle.doorlockCombo);
if ($.inArray(vehicle.tHarness, lists.tHarness) < 0) lists.tHarness.push(vehicle.tHarness);
count++;
if (count >= max) {
index++;
updateExplodeDataStatus(index);
window.setTimeout(explodeDataStep, 10, index, max);
return;
}
}
finishExplodeData();
}
由于某种原因,当索引达到大约 480 时,我注意到 $(document).data('data') 中存储的一些数据就消失了,我一生都无法弄清楚为什么。
因此,这里有一些可能会导致答案的问题:
- 以这种方式使用 window.setTimeout() 是一个非常糟糕的主意吗?
- 使用 jQuery.data() 可以存储的数据量是否有限制?我的 XML 文件约为 100KB。
I'm working on a utility web app that to help manipulate some domain-specific XML data.
The flow goes like this:
- Load XML file
- Parse XML file using the browser's native XML objects (not jQuery!) and convert into JavaScript object.
- Store resulting object using $(document).data()
- Iterate through object and extract additional information, storing that in another $(document).data() slot
#4 takes a fair amount of time, so I'm using window.setTimeout()
to split the work up into chunks.
Here is the function:
function explodeDataStep(index, max) {
var data = $(document).data('data');
var lists = $(document).data('lists');
$debug('explodeDataStep', index, $(document).data('data'), $.data(document));
var count = 0;
for (index; index < data.vehicles.length; index++) {
var vehicle = data.vehicles[index];
if ($.inArray(vehicle.make, lists.make) < 0) lists.make.push(vehicle.make);
if ($.grep(lists.model, function(v) { return v.make == vehicle.make && v.model == vehicle.model; }).length == 0) lists.model.push({ make: vehicle.make, model: vehicle.model });
if ($.inArray(vehicle.module, lists.module) < 0) lists.module.push(vehicle.module);
if ($.inArray(vehicle.doorlock, lists.doorlock) < 0) lists.doorlock.push(vehicle.doorlock);
if ($.inArray(vehicle.doorlockCombo, lists.doorlockCombo) < 0) lists.doorlockCombo.push(vehicle.doorlockCombo);
if ($.inArray(vehicle.tHarness, lists.tHarness) < 0) lists.tHarness.push(vehicle.tHarness);
count++;
if (count >= max) {
index++;
updateExplodeDataStatus(index);
window.setTimeout(explodeDataStep, 10, index, max);
return;
}
}
finishExplodeData();
}
For some reason, when the index gets up to around 480, I'm noticing that some of the data stored in $(document).data('data') just disappears, and I can't for the life of me figure out why.
So, here are some questions that may lead to the answer:
- Is using
window.setTimeout()
in this fashion an incredibly bad idea? - Are there limits as to how much can be stored using jQuery.data()? My XML file is ~100KB.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这很尴尬......
我在代码的其他地方使用了 Array.splice() 。这样就可以了。是的。
This is embarrassing...
I was using Array.splice() elsewhere in my code. That'll do it. Yep.
在这种情况下,我不会使用 jQuery,而是将您的信息存储为原生 JavaScript 对象。
In this case I would not use jQuery and instead store your information is native JavaScript objects.