简体   繁体   English

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

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

I'm struggling making the right piece of code with jQuery. 我正在努力用jQuery编写正确的代码。

In a nutshell, I want to check if something is completed via XHR request every 1000ms. 简而言之,我想每隔1000ms通过XHR请求检查是否完成了某些操作。 It does work perfectly on any desktop machine but does n ot work properly on latest raspberry pi with chromium installed. 可以在任何台式机正常运行,但不能装有铬的 最新树莓派上正常运行

The client have only one shot on getting HTTP/1.0 200 OK, the thing is the raspberry pi could miss it time to time and it's really annoying. 客户端在获得HTTP / 1.0 200 OK方面只有一招,问题是树莓派可能会不时错过它,这确实很烦人。

 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> 

Could anyone propose a better way to do this ? 有人可以提出一种更好的方法吗? Thank you! 谢谢!

Should not use setInterval, but setTimeout instead. 不应使用setInterval,而应使用setTimeout。

@Taplar : @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. async:false不仅导致请求是非异步的(与范例相反),而且还会在请求期间冻结浏览器,这会带来糟糕的用户体验。 Use setTimeout instead. 请改用setTimeout。 EDIT: Additionally async: false was removed in jquery 3.X 编辑:另外异步:在jquery 3.X中删除了false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM