AJAX Update live data

0

I am writing a system that should update two divs, as much as possible, in real time. The way I found it is as follows, using AJAX:

function actualizarDatos(){
	
	$.ajax({
		
		type: 'POST',
		cache: false,
		async:true,
		url: 'lista_datos1.php',
		success: function(respuesta) {
			
		$('#ListaDatos1').html(respuesta);
	   }
	});


	$.ajax({
		type: 'POST',
		cache: false,
    async:true,
		url: 'lista_datos2.php',
		success: function(respuesta) {
			
		$('#ListaDatos2').html(respuesta);
	   }
	});
	
}

setInterval( function(){
    
	actualizarDatos();
	
},3000)//Actualizo cada 3 segundos

I have already noticed some inconsistencies when using it and this web application will be executed by several simultaneous users.

Is what I'm doing right? Do you suggest a better way to do it?

I appreciate your comments, regards!

    
asked by pointup 25.09.2018 в 15:05
source

1 answer

1

I recommend that you have a single ajax call within the%% co_fucnión and that in the actualizarDatos() code you return the two data you want to update.

function actualizarDatos(){

    $.ajax({

        type: 'POST',
        cache: false,
        async:true,
        url: 'lista_datos1.php',
        success: function(respuesta) {
          //Suponiendo que devuelves en un array los datos
          $('#ListaDatos1').html(respuesta[0]);
          $('#ListaDatos2').html(respuesta[1]);
       }
    });

}

setInterval( function(){

    actualizarDatos();

},3000)//Actualizo cada 3 segundos

Depending on the way you want to return the data of your php you could adapt the function php .

    
answered by 25.09.2018 / 15:14
source