每隔X分钟/秒重新加载AJAX数据,jQuery

时间:2021-10-04 01:08:17

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 into a sidebar on the main page and load it via AJAX every 30 seconds (or some set period of time).

我编写了一个CMS,其中包含最近登录系统的人员日志。目前,这些数据通过Ajax提供给jQuery UI选项卡。我想将这些信息放在主页的侧边栏中,并每隔30秒(或一段设定的时间)通过AJAX加载它。

How would I go about doing this? Does the PHP response need to be JSON coded? I am fairly new to AJAX and JSON data.

我该怎么做呢? PHP响应是否需要进行JSON编码?我对AJAX和JSON数据还不熟悉。

Here is the PHP I am currently using to pull details from the users table-

这是我目前用来从用户表中提取详细信息的PHP-

<?php
$loginLog = $db->query("SELECT name_f, name_l, DATE_FORMAT(lastLogin, '%a, %b %D, %Y %h:%i %p') AS lastLogin FROM user_control ORDER BY lastLogin ASC LIMIT 10");
while ($recentLogin = $loginLog->fetch()) {
echo $recentLogin['name_f'] . " " . $recentLogin['name_l'] . " - " . $recentLogin['lastLogin'];
}
?>

Thanks! UPDATE

谢谢! UPDATE

Okay, this is what I have so far.. the part I'm stuck on is how to loop through JSON and enter it into the box. It works fine as long as I use just one result and assure it is not in [ ]'s. I'm just learning Ajax and JSON, for some reason it isn't coming to me too easily.

好吧,这就是我到目前为止......我坚持的部分是如何循环JSON并将其输入框中。只要我只使用一个结果并确保它不在[]中,它就可以正常工作。我只是学习Ajax和JSON,由于某种原因它不太容易找到我。

Javascript -

Javascript -

$(document).ready(function(){

                function refreshUsers(){

                    $.getJSON('json.php', function(data) {

                            for (var i = 0; i < data.length; i++) {

                                $("#loadHere").html('<p>' + data.name + ' ' + data.lastLogin + '</p>');

                            }

                });

            }

                var refreshInterval = setInterval(refreshUsers, 30 * 1000);

                refreshUsers();

            });

What my PHP script outputs -

我的PHP脚本输出什么 -

[{"name":"Joe Smith","lastLogin":"Fri, May 21st, 2010 08:07 AM"},{"name":"Jane Doe","lastLogin":"Fri, May 21st, 2010 07:07 AM"}]

Thanks!

谢谢!

2 个解决方案

#1


16  

PHP side, use json_encode().

PHP方面,使用json_encode()。

Client side, use $.getJSON():

客户端,使用$ .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 :)

有了这两个,你应该有很多东西可以开始使用。更重要的是,你要求我们为你工作:)

#2


2  

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

每隔30秒,这将加载PHP脚本的输出,并将该输出放入ID = whereIWantToDisplayIt的元素中

I prefer Seb's answer though.

我更喜欢Seb的答案。

#1


16  

PHP side, use json_encode().

PHP方面,使用json_encode()。

Client side, use $.getJSON():

客户端,使用$ .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 :)

有了这两个,你应该有很多东西可以开始使用。更重要的是,你要求我们为你工作:)

#2


2  

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

每隔30秒,这将加载PHP脚本的输出,并将该输出放入ID = whereIWantToDisplayIt的元素中

I prefer Seb's answer though.

我更喜欢Seb的答案。