从PHP / w获取两个ajax调用的状态

时间:2018-12-01 13:26:23

标签: javascript ajax

在我的php后端中给出两个api端点:

长功能端点:/_api/_long_function& 状态端点:/_api/_status_long_function

LongFunction中,我写会话数据,在StatusFunction中,我读取它们并将它们输出为json。我认为这与问题无关,但是如果需要的话,我也可以说明会议的开始和设置。

我现在想通过ajax调用启动long函数,然后定期通过第二个ajax调用请求状态。

JS代码

$(document).on("click", "#submitSummoner", function () {

    ...INIT VARS...

    /**
     * Periodically check Session Status
     */
    var to,
        clearTime = false,
        called = 0,
        set_delay = 1000,
        callout = function () {
            console.log('Called SessionCheck ' + called);
            $.get($url_session, {
                my_data: my_data
            })
                .done(function (response) {
                    if (called === 0) {
                        console.log('Called LongFunction');
                        $.get($url, {
                            my_data, my_data
                        }).done(function (data) {
                            console.log('Finished Request');
                            if (to) {
                                clearTimeout(to);
                                clearTime = true;
                            }
                        }).fail(function () {
                            if (to) {
                                clearTime = true;
                                clearTimeout(to);
                            }
                            console.log('Failed Request');
                        });
                    }
                    called++;
                    // This is the output I need
                    console.log('Current Status: '+response.status);
                })
                .always(function () {
                    if (clearTime === false) {
                        to = setTimeout(callout, set_delay);
                    }
                });
        };

    callout();
});

不幸的是,现在发生的事情是

控制台输出

1: Called SessionCheck 0

2: Called LongFunction(紧随其后)

3: Current Status: Whatever(立即)

4: Called SessionCheck 1(立即)

LONG PAUSE UNTIL LONG FUNCTION FINISHED

5: Current Status: Finished

Finished之后,setTimout将按预期清除。

但是为什么setTimeout不会每1000ms调用一次,而long函数需要10000ms +?

似乎第二个SessionCheck等待Long Check完成,我不知道为什么?在longFunction期间服务器是否可能“挂起”,CPU / RAM似乎正常。其他任何可以“锁定”它的东西

即使php会话检查功能崩溃,该功能也应该在1000毫秒后重试吗?

任何提示表示赞赏!

解决方案

我发现,会话未正确保存在LongFunction中:https://codingexplained.com/coding/php/solving-concurrent-request-blocking-in-php

1 个答案:

答案 0 :(得分:1)

我已经使用jQuery Promise重新编写了您的解决方案,以模拟get请求,并且它可以按照您的要求工作。我相信这证明了问题出在后端,“短”获取请求没有解决第二次。请提供解决该请求的代码。

var to,
  clearTime = false,
  called = 0,
  set_delay = 1000,
  callout = function () {
      console.log('Called SessionCheck ' + called);
      
      var shortGet = $.Deferred();
      shortGet
          .done(function () {
              if (called === 0) {
               
                  var longGet = $.Deferred();
                  
                  console.log('Called LongFunction');
                  longGet.done(function (data) {
                      console.log('Finished Request');
                      if (to) {
                          clearTimeout(to);
                          clearTime = true;
                      }
                  }).fail(function () {
                      if (to) {
                          clearTime = true;
                          clearTimeout(to);
                      }
                      console.log('Failed Request');
                  });
                  
                  
                 setTimeout(function() {
                    longGet.resolve();
                 }, 10000);
              }
              called++;
              // This is the output I need
              console.log('Current Status: still going');
          })
          .always(function () {
              if (clearTime === false) {
                  to = setTimeout(callout, set_delay);
              }
          });
     setTimeout(function() {
        shortGet.resolve();
     }, 200);
  };

callout();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>