0

According to this post, you can keep a connection open with the server to listen for any changes. Is there any possible way to do it with jQuery's $.get() or $.ajax() methods?

Maytha8
  • 846
  • 1
  • 8
  • 26
  • 2
    You should have a look at [web sockets](https://html.spec.whatwg.org/multipage/web-sockets.html) instead for that type of requirement – Buddy Christ Apr 29 '20 at 08:53
  • 1
    There's nothing (that I could see, it's quite long) in that question that suggests you can keep a connection open. $.get is shorthand for $.ajax. Consider `$.ajax` as being like a sending a letter - you get back a distinct "package" or set of results - it's not like a pipe where more results can keep coming while you process them. – freedomn-m Apr 29 '20 at 09:02

1 Answers1

1

At the simplest level, put your AJAX call into a function, then create an interval:

setInterval(ajaxCall, 300000); //300000 MS == 5 minutes

function ajaxCall() {
     $.ajax({
        url: 'fetch_details.php',
        type: 'get',
        success: function(response){
         // Perform operation on the return value
         alert(response);
        }
    });
}
samtax01
  • 834
  • 9
  • 11
  • Is it the same thing with `$.post()`? Just wondering... – Maytha8 Apr 29 '20 at 08:56
  • Not really, But while $.ajax() is customizable, You can as well have it like this. $.post( "ajax/test.html", function( data ) { $( ".result" ).html( data ); }); Do you understand? – samtax01 Apr 29 '20 at 08:59
  • I know that the attributes are different. I'm just asking "Does it work if I replace it?" – Maytha8 Apr 29 '20 at 09:01
  • 1
    `$.get` and `$.post` are both shorthand for `$.ajax` with the `type:` set and slightly different parameters. "does it work if I replace it" - if you replace `$.ajax({type:'get'` with a `$.post` will depend on whether your server accepts get/post for that action, so not really a yes/no. – freedomn-m Apr 29 '20 at 09:03
  • Oh sorry, I forgot that I was asking about GET. – Maytha8 Apr 29 '20 at 11:16
  • It's alright... you can do the same for $.get(). Kindly rate the answer for others to benefit from it. – samtax01 Apr 29 '20 at 12:03