我正在用JQuery Mobile构建一个应用程序。我有一个名为list.php的页面,它使用php从服务器获取数据。我需要每15秒运行一次脚本,并在数据库中发生更改时通知用户。我是PHP和Javascript的初学者,所以我不知道如何做到这一点,也不知道这是否可能。
这是我目前正在使用的代码。
<?php
$number = $_GET['number'] ;
mysql_connect('localhost','username','password');
$link = mysql_connect('localhost', 'username', 'password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
//specify database
mysql_select_db("database") or die("Unable to select database");
// Build SQL Query
$query = "select * from table where tablenumber = '"$number'" and state = 0
order by time";
$result = mysql_query($query) or die("Couldn't execute query");
?>
<!DOCTYPE html>
<html>
<head>
<title>title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.css" />
<script src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
<script src="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.js"></script>
</head>
<body>
<div data-role="page">
<div data-role="header" >
<h1>Transpanel</h1>
</div>
<div data-role="content">
<div data-role="collapsible-set">
<?php
// display the results returned
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
?>
<div data-role="collapsible" data-theme="b" data-content-theme="d">
<h3><?= $row["OSO"] ?></h3>
<p><?=$row["AIKA"]?><p>
</div>
<?php } ?>
</div>
</div><!-- /content -->
</body>
</html>
只需使用AJAX,并通过setInterval('checkfornew()', 15000);
每隔15秒调用AJAX函数。因此:
function checkfornew() {
var http = new XMLHttpRequest();
var url = "get.php";
http.open("GET", url, true);
//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");
http.onreadystatechange = function() {
if(http.readyState == 4 && http.status == 200) {
if(http.responseText != "") {
alert(responseText);
}
}
}
http.send(params);
}
并将身体标签更改为
<body OnLoad="setInterval('checkfornew()', 1500);">
为什么不修改更新逻辑,使其启动通知用户的通知线程?以这种方式进行轮询是非常低效的。
服务器上的代码不能自己运行并通知浏览器更改,它根本没有内置在HTTP协议中。
一个简单的解决方案是每15秒重新加载一次页面:
<body onload="window.setTimeout(function(){document.location.reload(true);},15000)">
如果你想让浏览器在不重新加载页面的情况下获得更新,你应该研究AJAX。基本上,您设置了另一个可以返回更改的PHP页面,例如JSON格式。使用页面中的Javascript(例如jQuery库中的ajax
方法),您可以获得更改并让脚本对其做出反应。
如果您在linux机器上,请将脚本添加到cronjob
评论后编辑
# crontab -e
00 * * * * /usr/local/bin/php /home/john/myscript.php
或者如果它在url 上
00 * * * * lynx -dump http://www.thegeekstuff.com/myscript.php
为了每15秒更新一次,最好使用cron作业。为了在数据库更改时通知用户,以下是我的解决方案:创建另一个表,其中包含用户所做操作的记录。另一种选择是在数据库上实现散列函数,但我不知道它的效率有多高
我认为您正在尝试创建一个移动web应用程序。通过setTimeout()每隔15秒加载整个页面可能会给你的浏览器带来不必要的负载,就像每15秒点击浏览器刷新按钮一样烦人!!!相反,您所需要做的就是使用json web服务,并在jquery.getJSON方法中解析php-json响应。使用两个不同的文件:list.html for jquery&用于json web服务的list.php。下面是一个解析json的示例,响应结果:{key1:OSO;key2:AIKA}
$(document).ready(){
//always include jquery code in $(document).ready(){ your javascript here} loads faster
setInterval(function() {
$.getJSON('list.php', function(json) {
//assuming list.php resides with list.html directory
//console.log(json)
console.log(json.result.key1);
console.log(json.result.key2);
});
}, 15000); //request list.php every 15 seconds
});