Ajax Async: true, I need an example

Asked

Viewed 12,636 times

2

Hello, I just want an example for study purposes. I want the page to update automatically, for future chat, the page update asynchronously and also to update page in real time, where other people from the computer can see the data at the same time. All with enough doubt in Ajax with Jquery, so I am 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 very well to use this function, I need only a simple function to is updating the data without the user noticing. It could be anyone, but I understand Jquery but there are few subjects that teaches about ajax.

Update can be every 2 seconds...

  • 1

    Out of curiosity, have you read the documentation function? If yes, you can edit the question and add what are the real questions?

  • First @Alan the file codeajax.php will not run as you are trying to run javascript inside a file .php.

  • 1

    async: true is the initial value even if you do not indicate it. E async: false was removed from jQuery because it was a bad idea. I also don’t understand the question, you can explain better?

1 answer

4


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>

Browser other questions tagged

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