0

I had a code that processes a huge JSON array in a "for" loop.

for(var i = 0; item = arr[i]; i++) {
   //PROCESS item
}

The issue was with a Firefox browser. It shows a warning dialog that script is running too long. The following code was the solution

//not precise code example, but it's kind of.
delay(arr, 0);

function delay(arr, num) {
   //process array by parts with 50 elements in each part
   for(var i = 1; i <= 50; i++) { 
      //PROCESS arr[num];
      num += i;
   }
   if(num < arr.length) {
      setTimeout(function() { delay(arr, num); }, 100);
   }
}

I wondering if there is a more fabulous way to workaround complete UI freeze and make Firefox to don't show script debug dialog. Please advise. Thanks

2 Answers 2

2

If you are using Firefox, you might be looking for web workers, which allows you to spawn background threads.

Sign up to request clarification or add additional context in comments.

2 Comments

I was hoping that there is some cross-browser solution, because other browsers are getting frozen as well, just without dialog boxes. And the CPU load on user's PC is 100% of course.
As best I can tell Web Workers are supported in Chrome, IE10+, Safari, etc. See here: caniuse.com/#feat=webworkers
1

I think web workers are what you need.

Here you can find an good introduction to web workers: link

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.