1

Now I have been puzzling with this for some time. I got part of it together in jquery/ajax:

<script type="text/javascript">
$(document).ready(function(){
    jQuery.ajax({
        type: "GET",
        url: "sessioncheck.php",
        dataType:"json",
        success:function(response){
            if (response) {
                window.location.href = 'logout.php';
            }
            else {
                // Process the expected results...
            }
        }

    });
});
</script>

This works perfectly, but I would like this process to repeat itself every 30 seconds. Can someone please help me with that? I have read something about setinterval, but I cant seem to get it to work.

Thanks a lot in advance for all of your help!

ManouHH
  • 149
  • 4
  • 13
  • 1
    why? If you're doing session management, the server should already be able to take care of this by comparing last-active times on the session, you don't need the client to ping the server every X seconds. – Mike 'Pomax' Kamermans Jun 11 '13 at 01:47
  • 1
    Because if they login from another location, I want the old session to log off automatically. In other words, they can then only be logged in once. – ManouHH Jun 11 '13 at 01:48
  • 1
    If it is important, you should do that server-side as javascript is easily disabled. – jeroen Jun 11 '13 at 01:49
  • The site serves with streams and without javascript the stream will not load. So that isn't really a problem. It would be better to do this server side though, but I can't think of a way to do that.. – ManouHH Jun 11 '13 at 01:55

3 Answers3

10
$(document).ready(function(){
    setInterval(function() {
        jQuery.ajax({
            type: "GET",
            url: "sessioncheck.php",
            dataType:"json",
            success:function(response){
                if (response) {
                    window.location.href = 'logout.php';
                }
                else {
                    // Process the expected results...
                }
            }

        });
    }, 30000);
});
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    It should be noted that this method will initiate the request every 30 seconds, but the responses may be delayed or even returned out of order. – George Cummins Jun 11 '13 at 01:53
  • @GeorgeCummins If the server takes more than 30 seconds to respond to this, he probably has bigger problems to worry about. – Barmar Jun 11 '13 at 01:55
  • @GeorgeCummins Then just cancel the previous request on the next iteration. – Ja͢ck Jun 11 '13 at 02:00
4

Add a interval like this.

<script type="text/javascript">
$(document).ready(function(){
    setInterval(function(){
        jQuery.ajax({
            type: "GET",
            url: "sessioncheck.php",
            dataType:"json",
            success:function(response){
                if (response) {
                    window.location.href = 'logout.php';
                }
                else {
                    // Process the expected results...
                }
            }
        });
    }, 30000);
});
</script>

More info here: http://www.jquery4u.com/jquery-functions/setinterval-example/

transilvlad
  • 13,974
  • 13
  • 45
  • 80
2

Just do a setInterval...

  <script type="text/javascript">
   $(document).ready(function(){
   setInterval(function(){
    jQuery.ajax({
        type: "GET",
        url: "sessioncheck.php",
        dataType:"json",
        success:function(response){
            if (response) {
                window.location.href = 'logout.php';
            }
            else {
                // Process the expected results...
              }
           }
        });
    }, 30000);
  });
  </script>
Kylie
  • 11,421
  • 11
  • 47
  • 78