Ajax转PHP循环问题

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.

1个回答


如果您有类似的内容:当Chrome中的标签页处于非活动状态时,如何使setInterval也能正常工作? </ p>

如果您的脚本/ jsfiddle正在登录到控制台,只需将其路由到页面标题,例如。 document.title ='您的setInterval输出'; ..因此,在切换制表符等时,您最好能够检测到间隔循环正在做什么。</ p>
     </ div>

展开原文

原文

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.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问