Reload AJAX data every X minutes/seconds, jQuery

后端 未结 2 1605
情歌与酒
情歌与酒 2020-12-10 16:01

I programmed a CMS that has a log of who has recently logged into the system. Presently, this data is fed into a jQuery UI tab via Ajax. I would like to put this information

相关标签:
2条回答
  • 2020-12-10 16:09

    PHP side, use json_encode().

    Client side, use $.getJSON():

    function refreshUsers(){
      $.getJSON(url, postData, function (data, textStatus){
        // Do something with the data
      });
    }
    
    // Keep interval in a variable in case you want to cancel it later.
    var refreshInterval = setInterval(refreshUsers, 30 * 1000);
    

    With these 2, you should have a lot to get started with. More than this, you'd be asking us to work for you :)

    0 讨论(0)
  • 2020-12-10 16:25

    The simplest way (whether its the best way is subjective - for the use case you've presented I'd say its fine) would be to do:

    var updateInterval = setInterval(function() {
      $('#whereIWantToDisplayIt').load('/thePathToThatScript.php');
    },30*1000);
    

    Every 30 seconds, this would load the output of your PHP script, and put that output into the element with ID = whereIWantToDisplayIt

    I prefer Seb's answer though.

    0 讨论(0)
提交回复
热议问题