是否可以在一个页面中进行异步和同步xhr请求? [英] Is it possible to make async and sync xhr requests in one page?

查看:269
本文介绍了是否可以在一个页面中进行异步和同步xhr请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好。我正在使用jQuery Mobile来驱动页面和库的设置方式,它劫持了页面上的每个链接并使它们异步。我不希望JQM篡改出站链接。到目前为止,设置 $ .mobile.ajaxEnabled = false 的工作正常。问题是,我需要在一个间隔中发出异步请求。然后,禁用ajax请求意味着这些间隔持续时间内的页面块。对于慢速网络或者服务器响应速度不够快的人来说是坏消息。



我尝试过的事情:



为了解决这个问题,我尝试使用





Hello there. I'm using jQuery Mobile to power a page and the way the library is setup, it hijacks every link on the page and makes them asynchronous. I don't want JQM to tamper with outbound links. So far, setting $.mobile.ajaxEnabled = false works just fine. The problem is, I need to make async requests in an interval. Disabling ajax requests then means the page bricks for the duration of those intervals. Bad news for those on slow networks or if the server doesn't respond fast enough.

What I have tried:

To work around this, I tried using


$.get('/livePercentage/live')

	.then(function(data) {	

		$.mobile.ajaxEnabled = false; // disable it so jqm doesn't tie up our outbound links

		assignPerc(JSON.parse(data));
	})

	// subsequent
	.then(() => setInterval(function() {	

		$.mobile.ajaxEnabled = true;

		$.get('/livePercentage/live')

		.then(function(data) {	

			$.mobile.ajaxEnabled = false;

			assignPerc(JSON.parse(data));
		});
	},1000*60*4));





但它变得更糟,更糟糕。从本质上讲,每当我打算发起异步请求时,我都试图将同步性置于保持状态,然后再恢复它。我不认为有人会在后台请求正在进行时点击出站链接。



以上片段将页面冻结约4整分钟(这可能与setInterval函数的4分钟间隔。是否可以在允许用户交互的同时阻止来自jqm的干扰?



But it just got much, much worse. In essence, I'm trying to put the synchronicity on hold whenever I intend to launch an async request, then resume it afterward. I don't imagine anyone will click outbound links while background requests are ongoing.

The above snippet freezes the page for about 4 whole minutes (which could be related to the 4-min interval of the setInterval function. Is it possible to thwart the interference from jqm while still permitting user interactivity?

推荐答案

.mobile.ajaxEnabled = false 工作得很好。问题是,我需要在一个时间间隔内发出异步请求。禁用ajax请求然后意味着这些时间间隔内的页面块。对于那些在慢速网络上或者服务器没有的人来说是坏消息。回答得足够快。



我尝试过:



To解决这个问题,我尝试使用





.mobile.ajaxEnabled = false works just fine. The problem is, I need to make async requests in an interval. Disabling ajax requests then means the page bricks for the duration of those intervals. Bad news for those on slow networks or if the server doesn't respond fast enough.

What I have tried:

To work around this, I tried using



.get(' / livePercentage / live'

.then(< span class = code-keyword> function (data){
.get('/livePercentage/live') .then(function(data) {


.mobile.ajaxEnabled = false ; // 禁用它以便jqm不会占用我们的出站链接

assignPerc( JSON .parse(data));
})

// 后续
。那么(()=> setInterval( function (){
.mobile.ajaxEnabled = false; // disable it so jqm doesn't tie up our outbound links assignPerc(JSON.parse(data)); }) // subsequent .then(() => setInterval(function() {


这篇关于是否可以在一个页面中进行异步和同步xhr请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆