Ajax Javascript error

Asked

Viewed 41 times

-1

Good I have the following Javascript code

function fun() {
    $.ajax({
        url: 'aposta.php',
        success: function(a){
            if(a) {
                a = JSON.parse(a);
                $("#tempo").html(a.time);

                if (a.time === 0) {
                    $('#oi').attr('disabled', 'disabled');
                    setTimeout(function() {
                        $('#oi').removeAttr('disabled');
                    }, 6000);
                }

                $("#box").animate({
                    'background-position-x': numero(a.numero)
                }, 500);
            }
            else {
                $("#tempo").html("0");
            }
        }
    });
};
  setInterval(function(){
	 fun();

  }, 1000);
 </script>

What I intended was that setinterval that updates 1 in 1 second put, an html code, so that this html code is updated without refresh in case I give some change in the site.

Basically, what I want is to update this variable $balance in php every 1 second without having to give F5 on the site.

How can I do that?

Thank you.

  • What does an error in ajax have to do with updating real number?

  • None of the questions are very clear, and the problems seem to be the same (I thought this, perhaps because they are not clear). If you edit both, making it more clear what you want, I withdraw the vote to close.

  • I’ll edit, thanks for warning me diego.

  • I’ve already edited Diego

1 answer

1

setInterval is not ideal for this situation, it can bring the results in the wrong order if by some delay in the network the answer to a request that was made after get first or stack a lot of requests together.

You can request the result of the url by passing a callback function to get more organized:

function requestMyServerData(){
    //a função abaixo chama $.ajax por dentro, 
    //simplifica para esses casos mais simples
    $.get('aposta.php', onResultReturned);
}

jquery.

Then you implement this callback function:

function onResultReturned(data){
    //aqui 'data' será a mesma coisa que 'a' no seu exemplo
    //pode tratar o resultado e então chamar novamente a primeira função
    //isso garante não fazer uma requisição antes da outra retornar
    requestMyServerData();
}

The ideal even for this type of situation is to use push, ie, make the server is able to alert the client’s browser when there is an interesting change for it. Polling is out of date.

If you are using an IIS and . net server, you can use Signalr to communicate with clients who currently have the page open. signaling documentation

Using apache I don’t know, but there must be something similar.

Good luck!

  • Good Afternoon, n understood mt well, this updates in real time?

  • Yes, $.get is the same as $.ajax, only with fewer options, just to make it easier for those who don’t need to set anything up, just ask for the result of a url, without changing any package header, etc... And you could declare the callback function within the function argument in the same way as above, I just separated it to organize and explain better

Browser other questions tagged

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