Expected out of order display

Asked

Viewed 36 times

0

I’m sending requests, only this return comes in an unexpected order.

I need the update guarantee to come first that support and consulting.

There are times when it works, there are times when it doesn’t.

Unexpected return

inserir a descrição da imagem aqui

Expected return

inserir a descrição da imagem aqui

function requisicaoSincrona(categoria, modulo, destino, callback) {
	var xmlhttp = null;
	if (window.XMLHttpRequest) {
		// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp = new XMLHttpRequest();
	} else {
		// code for IE6, IE5
		xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
	}
	var values = {
		modulo: modulo,
		categoria: categoria
	};
	var myJsonString = JSON.stringify(values);
	xmlhttp.onreadystatechange = respond;

	xmlhttp.open('POST', 'classes/getData.php', true);
	xmlhttp.send(myJsonString);
	//xmlhttp.abort();
	//como o código executado na volta da requisição é a função respond, chamamos o callback aqui
	function respond() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			destino.innerHTML += xmlhttp.responseText;
		}
		//verifica se uma função foi de fato passada, para então chama-la
		if (callback) callback.call();
	}
}
var x = document.getElementsByClassName('terceiraEtapa')[0];
requisicaoSincrona(6, modulo, x, function() {
	requisicaoSincrona(9, modulo, x);
});

1 answer

0

I think your problem is just the asynchronous request, which is the default for browsers. Explanation

If you change the third entry of the next line of code to FALSE, I believe you will get the expected result always, but I do not say it is the best solution.

xmlhttp.open('POST', 'classes/getData.php', true);

I would try to return both HTML structures in the same request to the getData.php file.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.