Real time with javascript or jquery?

0

I need to enable message notifications on a webpage,

Example,

User receives a message and I notify him that he has a new message but does not refresh the page.

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

    
asked by anonymous 14.03.2017 / 20:56

1 answer

1

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 the clearInterval method. documentation here

clearInterval(intervalo );
    
14.03.2017 / 21:15