Ajax Async: true, I need an example

1

Hello, I just want an example for study purposes. I want the page to update automatically, to make future chat, the page update asynchronously and also to refresh real-time page, where other people on the computer can see the data at the same time. All with enough doubt in Ajax with JQuery, so I'm asking the question. My question is related to function ...

I have the test.php file

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
	<title></title>
	<style type="text/css">
	</style>
</head>
<body>

<div id="atualizar-assincrona"></div>

 <script type="text/javascript" src="jquery-3.2.1.min.js"></script>
 <script type="text/javascript" src="ajax.js"></script>

</body>
</html>

I have the ajax.php file

$(function(){
$.ajax({
    url: "ajax.php", // substitua por qualquer URL real
    async: true
}).done(function () {
    a = true;
});
	
});

Sorry but I do not know how to use this function, I need only a simple function to update the data without the user noticing. It may be either, but I understand JQuery but it has few subjects that teaches about ajax.

Update can be every 2 seconds ...

    
asked by anonymous 25.08.2017 / 23:46

1 answer

1

A simple example would be to create a function to call Ajax and return the data:

<script>
    $(function(){
        setTimeout("atualiza()",2000); // Aqui eu chamo a função após 2s quando a página for carregada
    });

    function atualiza(){ // Função com Ajax
    $.ajax({
        url: "teste.php", // substitua por qualquer URL real
        async: true
    }).done(function (data) { // "data" é o que retorna do Ajax
        a = true;
        $("#atualizar-assincrona").html(data); // Aqui eu jogo o retorno do Ajax dentro da div
        setTimeout("atualiza()",2000); // Novamente chamo a função após 2s quando o Ajax for completado
    });
    }
</script>
    
26.08.2017 / 19:10