Real time with javascript or jquery?

Asked

Viewed 888 times

1

I need to enable message notifications on a web page,

Example,

User receives a message and I notify him that he has a new message but without updating the page.

How can I make a function that runs every two or three seconds an ajax to check if there is a new message available?

  • 3

    If you want ajax use ajax Polling method, using the setInterval, the ideal however would be to use the Websockets, it is much more economical and performatic, however it is more complex and the PHP doesn’t handle it so well, but you can outsource this using Pubnub, Pusher, Ably.io, Scaledrone, Hydna, Google Drive Real Time...

  • Related: http://answall.com/questions/9475/notifications-tempo-real-simillate-ao-stack-overflow/10109

  • @Inkeliz thanks, I needed something really simple and your answer answers my question well.

  • 1

    websockets is a much better technology for this, but is only compatible with the most current browsers.

1 answer

2


use the setInterval function, documentation here

for example to perform its function every 3 seconds

<script type="text/javascript">
        <!--
        window.onload = function() {            

        function funcaoAjax(){
          console.log('Executando');
            

        }
            var intervalo = setInterval(funcaoAjax,3000);
}
        //-->
        </script>

to stop execution use clearInterval method. documentation here

clearInterval(intervalo );

Browser other questions tagged

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