0

I have a button that checks a lot (300+) posts for a specific value and other things (about 20 if, else's). Somehow the ajax call of the button stops after looping about 73 times/2mins.

It doesn't loop this ajax call, there's a PHP loop in the function it's referring to.

Is there any way to extend this? This is what I currently have:

$.ajax({
    url: ajaxurl + "?action=updatefield",
    type: 'post',
    data: dataString,
    success: function(data) {
        console.log("SUCCESS!");
        $("#myResponse").html("<h4>Response: </h4><hr>" + data);
    },
    error: function(data) {
        console.log("FAILURE");
    },
    timeout: 600000 // (this is what I tried, but it doesn't seem to work)
});

Perhaps this is the answer for my problem, but not my question: Browser Timeouts

Is there a way to extend this time, or is there another way?

Marthijn
  • 25
  • 4
  • 4
    That sounds like a server side timeout, not a client side/JS-related one. – Rory McCrossan Jan 04 '18 at 15:11
  • How do you call this? In a setTimeout or a loop or a click? Do you expect ONE result from the PHP? – mplungjan Jan 04 '18 at 15:13
  • i'm not sure what you mean by `ajax loop` but I assume at some point the code reaches the error or success handler. Inspecting the `data` should give you more details possibly showing if the server timed out? – Nope Jan 04 '18 at 15:14
  • 2
    If you have an AJAX call that takes more than 2 minutes to run, then your problem is not on the client side. – Alex Howansky Jan 04 '18 at 15:14
  • @mplungjan I call it with a click. It gives back some that appends to a debug div. – Marthijn Jan 04 '18 at 15:17
  • I agree with Rory and Alex, this sounds like your PHP process is taking longer than the Apache timeout setting allows so the Ajax returns nothing. You will probably have some Apache logs showing this timeout. Reviewing the logs will probably help you determine exactly what is going on. If this is the case than you will have to optimize your script to run faster, increase the Apache timeout period or use something like Websockets instead of Ajax. – splitwire Jan 04 '18 at 15:18
  • So what is the "stops after looping 73 times"? You mean the PHP stops and the JS does not show anything? – mplungjan Jan 04 '18 at 15:18
  • Possible duplicate of [How to increase the execution timeout in php?](https://stackoverflow.com/questions/3829403/how-to-increase-the-execution-timeout-in-php) – Grynets Jan 04 '18 at 15:19

2 Answers2

0

So let me get this right... you have ONE Ajax call that triggers a PHP loop, and it's the PHP loop that times out?

It could be:

  1. An HTTP timeout (this can be increased in the Apache config, but it's not recommended)
  2. An HTTP body size overflow (again this can be increased in the Apache config, but it's not recommended)
  3. A server-side limit on the maximum execution time of a PHP script (this can be changed in php.ini, but guess what... it's not recommended!)

Ultimately you are not doing this right. You should be calling the PHP script every so often (for example every second) by putting the Ajax call in a JS setInterval(1000); The PHP script itself should be quick and to the point.

0

I have tracked down the issue by enabling PHP errors. Besides fixing common errors, I found the issue.

Allowed memory size of 134217728 bytes exhausted

I'm currently trying to clean up my code and remove any unnecessary requests to increase speed and efficiency. Thanks for all the answers.

Marthijn
  • 25
  • 4