Ajax to PHP Looping Issue-ThrowExceptions

Exception or error:

I have an issue with a continuous Ajax to PHP loop. The code (below) works absolutely fine. However I find it will run for 2, 3, or more hours or as required.. but then have an “x minute” break where it won’T run or produce any logs… before once “again” resuming for 2, 3 or more hours.

$.ajax({
    async: false,
        type: "POST",
        url: ajaxURL,
        dataType: 'json',
        data: {functionname: 'conditionTrade', arguments: [c_piars, c_timeframes, usd_amount ,stop_loss, stop_loss_min, stop_loss_max]},
// post to php and check condition
        success: function (data) {
            if( !('error' in data) ) {
                var results = data["result"];
                if (results) {
                    $( "#tradelog" ).load( "tradelog.html" ); // <--- produce a log
                    var pft = c_piars+"profitloss.txt";
                    $( "#profit" ).load( pft );
                    checkCondition(); 
// recursion call to this same method ie. check again
                }
                else {
                    console.log(data.error);
                }
            var objDiv = document.getElementById("tradelog");
            objDiv.scrollTop = objDiv.scrollHeight;
        }},
        error: function(jqXHR, textStatus, errorThrown) {
            console.log("Error[refresh]: " + textStatus);
            console.log(jqXHR);
            checkCondition(); 
// recursion call to this same method ie. check again
         },

    });

Please can somebody advise as to why this loop is randomly breaking, and then continuing on its own.
The loop is intended to retrieve candlestick data (approximately every 5 seconds), then perform some Technical Analysis checks, before finally.. outputing a log of the whether the TA conditions were found. The code runs fine.
However it’s almost as if when I’m not physically on the browser page, then, it may randomly stop working for 15 minutes or a random time, before resuming on its own. (Hence I may get 2,3,4,5 hours of continuous checks, then a random dropout of 30 minutes to 2 hours, before resuming checks for x hours). What am I missing? I’ve looked into using different browsers, browser resources, internet connections, php timeouts etc. but this one is really head scratching. Thank you if you can help.

Ps. With a previous version I used to used the ajax- settimeout (5000) looping method, rather than the “recursion call to this same method” shown. This would literally run all day night without any dropping out – but I found that it would race itself from time to time, not waiting for the previous loop to complete. And therefore I might find 2 or 3 log entries for the same loop. How might I stabalize this loop to perform as I would like ? Thanks.

How to solve:

If you have similar: How can I make setInterval also work when a tab is inactive in Chrome?

If your script/jsfiddle is logging to the console, simply route it to the page title eg. document.title= ‘your setInterval output’; ..so you’ll better be able to detect what your interval loop is doing as you switch tab etc.

Leave a Reply

Your email address will not be published. Required fields are marked *