I have a script (not Nodejs ) in which there are a lot of ajax requests. All this works well, except that after 3000 the browser crashes. (optional after 3000. this is for example.)

And I have a question. Is it possible to somehow run the script outside the browser? Better yet, run in a browser, but execute outside the browser.

What I already did:

  1. Use async:false . Everything works fine, but the interface of the page is freezing, and requests are executed quite slowly. I also think that when using callbacks instead of async:false , the script execution will remain the same. The interface will probably remain working, but the speed will remain the same and this will make the coding much harder for me.
  2. Using the headless browser . I tried to use the Rhino engine in the console under Windows . I rewrote the code for this case, but he began to produce such a number of errors (and quite insignificant when executing the code in the browser), that after, somewhere, 15 I gave up on this method with my hand. I used Rhino with Envjs and jQuery ).
  3. Packing Javascript in a Windows application. I tried, but it's not at all.

The structure of the code now looks like this:

 $.ajax({ url: "ajax/example.php", success: function(data){ $.ajax({ url: "example.php", success: function(data){ }); }, complete: function(){ $.ajax({ url: "example.php", success: function(data){ }); } ); 

I do not intend to rewrite code on Nodejs or any other language. And I almost forgot, Firefox browser (maybe needed). The system is pretty weak. The error generated when the browser is more beautiful is related to jquery.js, respectively.

  • one
    and what kind of error when the browser is more beautiful, write in full? It does not provide any information связана с jquery.js . And on the topic, why do you use so many requests, what is their role? - Vasily Barbashev
  • Now I can not simulate a mistake, because I am not at my computer. But I am 100% sure that crash occurs only because of the huge number of requests. And I use this amount in obtaining information for my bot. This is also not the point. It is important that the requests are very, very much, and about 4 per second with the current code. - el pax
  • I'm just trying to find an alternative to async: false / callbacks. And let's say we standardize one ajax request, stuff it into one function with a callback, I can't either. Because I have 5 function blocks with queries. 2-3 requests nested in each. And each request is written to fit your needs. Perhaps I am not the best way to describe the question, forgive this. But the main question is how to slow down the sending of requests. (I think no need to offer setTimeOut) - el pax
  • 3000 synchronous requests? - etki
  • Now it works like 10,000+ asynchronous requests with pseudo-timing due to the success / complete structure. (since complete is started after success, I have time to get the necessary information and redirect to where I need it) - el pax

1 answer 1

In order not to freeze the interface, use web workers for "hard" work. This is separate from the browser page thread that executes your JS.

You'll have to rewrite your code a bit - to communicate with the Worker via the postMessage() and the message event.


To optimize in fact sequential ajax requests, try re-using the XMLHttpRequest object. It is possible that with each request a new one is being created now, taking up memory, and with thousands of requests, the memory quickly goes away. See the xhr field of jQuery.ajax() parameters.