How to break out of ajax hanging call and load another page

I am developing an account application. I am currently using OO php to create invoice objects. The objects themselves contain objects for customers, products, invoice details, firms.

Now I am working on a page to review. There was a problem when I had too many invoices (tested with only 1,500 fictitious invoices, which could have been much longer in time), creating php objects took about 7 seconds. I feel this is too long because it is for just one request. In addition, since php works with servers, the page did not load anything before all the objects were built. I looked at the blank screen for 7 seconds and then got everything in an instant (everything is on the local host, so it should be worse online).

Since the page should have more functionality, but just be a review (i.e. create new invoices using filters to narrow down the invoices issued), and I do not want the user to have to wait until invoices are created before they can use other functions, I changed the way the page works.

Now I first load the main html structure and only then start to get my account data by calling $ .ajax (). I built ajax_loader to notify the user that something is happening. When the call is made, the data is displayed, but I still have a problem that the user cannot do anything. He can click the link / button or whatever, but it doesn’t β€œact” until my ajax calls are completed. Once the calls are completed, everything works. When a link is clicked during an active call, the 'click' event is logged, but the trigger only occurs when ajax is executed.

The problem has nothing to do with my synchronized ajax calls. If anyone has any suggestions for solving this problem, I will be very grateful to them. My first thoughts will override ajaxcalls, but from what I have read so far, I suspect that the abort () function will not do the job.

edit Making a few more attempts, I noticed that everything works while ajaxcalls is still running, with the exception of loading a page from my own site (domain, server, or whatever I call it) or making any other ajaxcall that includes the same server that is:

$("#dummyButton").click(function(){ window.location='http://www.google.com' //works window.location='index.php' //doesn't work alert("test"); //works console.log("test"); //works }) a href='http://www.google.com' //works a href='index.php' //doesnt work 

So, I assume that the server is busy building invoice objects, so it will not accept a new request. The following is added to this conclusion:

  console.log("start slow call"); slow = $.ajax({ a very slow/heavy call success:function(){ console.log('end slow call'); } }); console.log('start fast call'); quick = $.ajax({ a very quick/lightweight call success: function(){ console.log('end fast call'); } }); 

When I make these two calls at the same time, the fast call will not end until the slow call is completed:

console prints:

 start slow call start fast call end slow call end fast call 

executing both at the same time makes a quick call lasting 5 seconds (according to Firebug) when only a quick call is completed, ending in 150 ms

I would already have guessed that several ajaxcalls would run in parallel, rather than serial.

 abort() function: (having my 2 ajaxcalls as globals so i can abort them); $("a[href]").click(function(){ slow.abort(); quick.abort(); window.location = "index.php"; return false; }) 

causes ajaxcalls to interrupt, but still the server continues to execute code from both calls, so it will not accept my request to go to index.php until the server code from ajaxcalls is complete.
after about 5 seconds (counting in the head) my index.php will start loading

Therefore, the following question arises:

Is there any managed way to cancel all the processes that the server starts for this particular client?

Other thoughts that ultimately were not the cause:

I have already set up my account constructor, passing it a boolean value to determine if the object requires all the information or only basic information. This made my ajaxcall (or better the server process behind a specific ajaxcall) about 3 seconds shorter (on 1500 dummy accounts). I could set up a database and therefore many other things on already developed materials. Or, since creating all invoice objects is part of the time, could I just do it in a non-OO way?

This is sad because of me: this was my first real OOP project, which is easy to work with, but apparently there are a lot of problems when calculating the time when working with a decent amount of objects.

+4
source share
1 answer

Hey, I just read your problem very quickly - so I'm jumping, I'm not from here with my answer.

When you call two php requests at once, and one does not end before the other, that is, it is likely that a fast request will not be able to start a session (session_start ()) before a slow request closes it.

Try closing the session if you no longer need it before starting any lengthy process.

+5
source

Source: https://habr.com/ru/post/1343305/


All Articles