0

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.

metokitt
  • 53
  • 7
  • You may be able to get good insight by using your browsers web developer debugging tools and viewing the network tab. Are the ajax calls completing as expected? Possibly they are hanging up and not finishing? In general it's hard to have a constant feed of quickly changing data such as candles like this. If you can find an alternative data provider which offers a stream of data rather than an http get based pull, you will be better off in the long run. – James Clark Mar 07 '20 at 14:56
  • try to reduce the code to only the minimum that exhibits the problem. That might help you to understand it. – Tomáš Pospíšek Mar 07 '20 at 15:05
  • Thank you James Clark.. When I previously looked at the network tab it was very 'congested' as you might expect with a call every 5 seconds...and writing to the .log and profit/loss file(s). I noticed that most Status calls were "200" however there exists a backlog of Status:"pending" requests also. Might there be a workaround that could eliminate pending requests and perhaps free up browser resources in the process ? – metokitt Mar 07 '20 at 15:25
  • I totally accept the advices to look for a stream / reduce the code... However I'm 1 year into attempted php developing, and this is the ONLY issue my project finally has. I'd say it was 90% complete - so basically I'm resisting having to start from scratch with a new architecture, if I can. – metokitt Mar 07 '20 at 15:27
  • Okay thanks guys. I'll try to check the php - timeDiff() between log outputs somehow.. and restart the bot. – metokitt Mar 08 '20 at 10:02
  • @TomášPospíšek this is crazy problem, it's almost as if when I leave the page, to go refresh and view the logs or do something in another window.. something gets triggered I can't see. I imagined expert developers might have observed or come across something like this in the past. – metokitt Mar 08 '20 at 10:09
  • The point I was trying to make is not to start from scratch, but in order for you (and us) to be able to debug the **problem** to reduce the code as much as possible to retain only the minimum amount of code that displays the problem. That way you eliminate all the irrelevant stuff. That does not mean that you discard all that you have created and start from scratch. It's only to to find the problem. Afterwards you can continue with all the stuff you've already done. Do you understand this? This is crucial engineering practice and strategy! – Tomáš Pospíšek Mar 08 '20 at 20:43
  • @TomášPospíšek Thank you, I understand. I did as you suggested and indeed noticed some bugs.. so researched and rewrote all the code. I discarded the recursion.. tried various strategies.. async: true false.. request abort.. timeout.. before send.. set script time limits, execution times etc. I finally stumbled on the solution.. https://stackoverflow.com/questions/6032429/chrome-timeouts-interval-suspended-in-background-tabs/31105370#31105370 – metokitt Mar 17 '20 at 11:27
  • if any of the comments were helpful than you might want to consider to give them a "flag up" to encourage the commenters to help others in the future – Tomáš Pospíšek Mar 17 '20 at 13:37

1 Answers1

1

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.

metokitt
  • 53
  • 7