jQuery .ajax 通过 setInterval 的 XHR 请求在 Raspberry PI Chromium 上无法正常工作

jQuery .ajax XHR request via setInterval doesn't work properly on Raspberry PI Chromium

我正在努力用 jQuery 编写正确的代码。

简而言之,我想每 1000 毫秒通过 XHR 请求检查是否完成了某项操作。它在任何台式机上都能完美运行,但在最新的 raspberry pi 和 chromium[=] 上不能正常运行 32=]已安装。

客户只有一次机会获得 HTTP/1.0 200 好的,问题是 raspberry pi 可能会时不时错过它,这真的很烦人。

var my_interval = null;

var k = function()
{

  $.ajax({
            url: '/my-ext-request',
            async: false,
            type: 'GET',
            success:  function(data) 
            {
            console.log('Success!!');
            clearInterval(my_interval);
            },
            error: function(jHXR, exception, m)
            {
            console.error('Error!');
            }
        });

}

$(function() {
   my_interval = setInterval(k, 1000);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

谁能提出更好的方法来做到这一点? 谢谢!

不应使用 setInterval,而应使用 setTimeout。

@Taplar :

Don't do that. async: false not only causes the request to be non asynchronous, which is counter to the paradigm, but also freezes the browser for the duration of the request, which is terrible user experience. Use setTimeout instead. EDIT: Additionally async: false was removed in jquery 3.X