Is it possible to make async and sync xhr requests in one page?

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.

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));

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. Is it possible to thwart the interference from jqm while still permitting user interactivity?

One CAN make async and sync requests. However, if you have DevTools open when accessing a page that has sync requests, there is a message that states that sync requests are deprecated and async should be used. So, caveat emptor.

V/r,

^ _ ^

You don’t understand. I intend to make async requests to links navigating within the page e.g javascript links, jqm specific links etc AND the specific url in that snippet in my OP. For every other link (that is not an XHR), I want the default behaviour of actually departing the page. The problem is that jqm lumps everything together as async and attempts to load those external links into the current page (instead of the desired behaviour I have described).

I’m trying to suspend the jqm interference and make my async XHRs, then return control to jqm to hijack other inbound links as it pleases.

I don’t think that you can mix async and sync. Even if JavaScript were to allow it, I think jQuery will override and make them all async if for no reason other than sync is “bad form”.

My assessment could be incorrect, though.

V/r,

^ _ ^

You’re right. I didn’t mean synchronous Ajax requests otherwise, I’d have seen this message. What I intend to make is regular requests/page navigation (instead of the prevent default they’re using to hijack my links after their Dom manipulations). If I click on “/Xxx”, I want to leave the current page and go to mysite/xxx rather than jqm loading “/xxx” asynchronously. The only links I want jqm to attach events to are page anchors like “#section5” but there’s no way I know of to do specify links to it. It automatically prevents default on all links and tries loading them asynchronously. I can’t remove the listeners on these links because they’re anonymous so my best bet is to disable asynchronous altogether.

The problem with this approach is that I’m having a hard time restarting and repausing asynchronicity. Get it?

I get it. I’m just not sure it’s possible.

V/r,

^ _ ^

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.