简体   繁体   English

JavaScript更新2MB JSON文件

[英]JavaScript update a 2MB JSON file

How to update a 2MB JSON file using JavaScript? 如何使用JavaScript更新2MB JSON文件?

I use phonegap to develop an Android application. 我使用phonegap开发Android应用程序。 Now, I need retrieve data from a JSON file and then update data back into this JSON file. 现在,我需要从JSON文件中检索数据,然后将数据更新回该JSON文件中。

It shows me that the " JavaScript execution exceeded timeout " 它向我显示“ JavaScript执行超出超时

Most JavaScript engines have an execution limit, meaning that they will throw an error when your code runs for longer than x seconds. 大多数JavaScript引擎都有执行限制,这意味着当您的代码运行时间超过x秒时,它们将引发错误。 In most browsers, this is just 5 or 10 seconds. 在大多数浏览器中,这只是5或10秒。 Have a look at this answer on SO for more information. 有关更多信息,请参见SO上的此答案

Three solutions come to mind: 想到三个解决方案:

  • Break up the long-running code in smaller pieces. 将可长时间运行的代码分成小段。 Rewrite your algorithm so it works in short "spurts" and use setInterval or setTimeout to call the code in iterations. 重写您的算法,使其简短地起作用,并使用setIntervalsetTimeout在迭代中调用代码。
  • Use Web Workers , which are specifically designed for long-running tasks. 使用Web Workers ,它是专门为长时间运行的任务而设计的。 The downside? 不足之处? Browser compatibility is only so-so. 浏览器兼容性只是马马虎虎。
  • Run it on a server . 在服务器上运行它。 This may be either trivial or nearly impossible, depending on your problem. 根据您的问题,这可能是微不足道的或几乎是不可能的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM