大json(如100MB)在javascript中表现出色
我不熟悉REST技术。我目前正在处理来自服务器的json响应,并在客户端显示数据 我现在得到大约22MB的json数据,我需要将其导出到excel工作表中 我的问题是: 当我迭代到json时,浏览器没有响应。我做错了什么?理想情况下,您不应该导出这么大的excel文件供下载。您必须将数据分块并要求用户分批下载。然而,如果这可能是您需求的一部分,要处理它,请参考下面的答案 您可能需要批量下载json,并将其合并为单个excel文件,然后要求用户下载 “无响应脚本”对话框显示某些javascript线程花费的时间太长或完成的时间太长。编辑注册表可以工作,但您必须在所有客户机上进行编辑。您可以使用如下递归闭包来缓解问题。它只是一种编码结构,允许您长时间运行for循环,并将其更改为可以执行某些工作的内容,并跟踪它停止的位置,让位于浏览器,然后继续它停止的位置,直到我们完成为止 图1,将这个实用程序类RepeatingOperation添加到javascript文件中。您无需更改此代码: 图2,以下代码表示导致“停止运行此脚本”对话框的代码,因为它需要很长时间才能完成: 图3。下面的代码修复了图2中有问题的代码。请注意,for循环被一个递归闭包所取代,该闭包每隔10次heavytask迭代将控制权传递回浏览器 根据这一来源改编:大json(如100MB)在javascript中表现出色,javascript,Javascript,我不熟悉REST技术。我目前正在处理来自服务器的json响应,并在客户端显示数据 我现在得到大约22MB的json数据,我需要将其导出到excel工作表中 我的问题是: 当我迭代到json时,浏览器没有响应。我做错了什么?理想情况下,您不应该导出这么大的excel文件供下载。您必须将数据分块并要求用户分批下载。然而,如果这可能是您需求的一部分,要处理它,请参考下面的答案 您可能需要批量下载json,并将其合并为单个excel文件,然后要求用户下载 “无响应脚本”对话框显示某些javascript
你的问题是什么?这可能会帮助您了解主题,以便我们能够为您提供帮助。在BatchesBrowser中迭代JSON,如果完成了大量处理而没有返回浏览器响应,则挂起并显示“停止慢速运行脚本”对话框。请参考我的答案以避免脚本运行缓慢。您到底想要什么文件类型?你是怎么做到的,有代码吗?你希望推出什么样的结构?请仔细描述,让他人理解您的问题。快点,否则有人会投票否决你;通过回答,我有了一些想法……谢谢:
RepeatingOperation = function(op, yieldEveryIteration) {
//keeps count of how many times we have run heavytask()
//before we need to temporally check back with the browser.
var count = 0;
this.step = function() {
//Each time we run heavytask(), increment the count. When count
//is bigger than the yieldEveryIteration limit, pass control back
//to browser and instruct the browser to immediately call op() so
//we can pick up where we left off. Repeat until we are done.
if (++count >= yieldEveryIteration) {
count = 0;
//pass control back to the browser, and in 1 millisecond,
//have the browser call the op() function.
setTimeout(function() { op(); }, 1, [])
//The following return statement halts this thread, it gives
//the browser a sigh of relief, your long-running javascript
//loop has ended (even though technically we havn't yet).
//The browser decides there is no need to alarm the user of
//an unresponsive javascript process.
return;
}
op();
};
};
process10000HeavyTasks = function() {
var len = 10000;
for (var i = len - 1; i >= 0; i--) {
heavytask(); //heavytask() can be run about 20 times before
//an 'unresponsive script' dialog appears.
//If heavytask() is run more than 20 times in one
//javascript thread, the browser informs the user that
//an unresponsive script needs to be dealt with.
//This is where we need to terminate this long running
//thread, instruct the browser not to panic on an unresponsive
//script, and tell it to call us right back to pick up
//where we left off.
}
}
process10000HeavyTasks = function() {
var global_i = 10000; //initialize your 'for loop stepper' (i) here.
var repeater = new this.RepeatingOperation(function() {
heavytask();
if (--global_i >= 0){ //Your for loop conditional goes here.
repeater.step(); //while we still have items to process,
//run the next iteration of the loop.
}
else {
alert("we are done"); //when this line runs, the for loop is complete.
}
}, 10); //10 means process 10 heavytask(), then
//yield back to the browser, and have the
//browser call us right back.
repeater.step(); //this command kicks off the recursive closure.
};