87,917
社区成员
发帖
与我相关
我的任务
分享
function ajax_loadTable(){
//中间还有很多代码
if(settimeout_id!=null && settimeout_id!="")
{
clearTimeout(settimeout_id); //清除settimeout函數產生的內存消耗.
}
settimeout_id=setTimeout("ajax_loadTable()",5000); //設置自動刷新.
}
$(doucment).ready(function(){
if(isIE){CollectGarbage("collect");} //清除運行之前的內存.
// 每隔 10 秒释放一次内存
if(isIE){window.setTimeout("CollectGarbage();",5000);}
ajax_loadTable();
});
//a.html(发送消息)
<textarea id="msg" rows="5" cols="30"></textarea>
<input type="button" value="发送" onclick="send()"><span id="sendmsg"></span>
<script src="../jquery.js"></script>
<script>
function send(){
var msg = document.getElementById("msg").value;
if(!msg)document.getElementById("sendmsg").innerHTML = "消息不能为空";
document.getElementById("sendmsg").innerHTML = "正在发送...";
$.post("server.php", {"msg": msg, "rand": Math.random()}, function(data){
if(data == "1"){
document.getElementById("sendmsg").innerHTML = "发送成功!";
}else{
document.getElementById("sendmsg").innerHTML = "发送失败!";
}
});
}
</script>
//b.html
<div id="div"></div>
<script src="../jquery.js"></script>
<script>
function text_replace(text){
var l = text.length;
for (var i = 0; i < l; i++) {
if (text.charAt(i) == "%"){
if(text.charAt(i+1) == "2"){
if(text.charAt(i+2) == "3"){
text = text.replace("%23","#");
}
if(text.charAt(i+2) == "4"){
text = text.replace("%24","$");
}
if(text.charAt(i+2) == "6"){
text = text.replace("%26","&&");
}
if(text.charAt(i+2) == "B"){
text = text.replace("%2B","+");
}
if(text.charAt(i+2) == "C"){
text = text.replace("%2C",",");
}
if(text.charAt(i+2) == "F"){
text = text.replace("%2F","/");
}
}
if(text.charAt(i+1) == "3"){
if(text.charAt(i+2) == "F"){
text = text.replace("%3F","?");
}
if(text.charAt(i+2) == "B"){
text = text.replace("%3B",";");
}
if(text.charAt(i+2) == "D"){
text = text.replace("%3D","=");
}
if(text.charAt(i+2) == "A"){
text = text.replace("%3A",":");
}
}
if(text.charAt(i+1) == "4"){
if(text.charAt(i+2) == "0"){
text = text.replace("%40","@");
}
}
}
}
return text;
}
var filemtime = 0;
function test(){
$.post("server.php", {"filemtime": filemtime, "rand": Math.random()}, function(data){
if(data){
data = eval("(" + data + ")");
filemtime = data["filemtime"];
document.getElementById("div").innerHTML = text_replace(decodeURI(data["msg"]));
setTimeout("test()", 1000);
}else{
alert("error");
}
});
}
test();
</script>
//server.php
<?php
function DBConnect(){
mysql_connect('192.168.1.2', 'db_user', 'db_pass');
mysql_select_db(db_name');
mysql_query('set names gbk');
}
if(isset($_POST['msg'])){
$msg = iconv('utf-8', 'gbk', htmlspecialchars($_POST['msg']));
DBConnect();
mysql_query('insert into test(msg) values("'.$msg.'")');
if(mysql_insert_id()){
$fp = fopen('msgfile.txt', 'a');
fputs($fp, $msg, 1);
echo '1';
exit;
}
echo '2';
exit;
}
$filemtime = isset($_POST['filemtime']) && $_POST['filemtime']? $_POST['filemtime']: filemtime('msgfile.txt');
while(1){
if($filemtime == filemtime('msgfile.txt')){
sleep(1);
clearstatcache();
}else{
DBConnect();
$rs = mysql_query('select msg from test order by id desc limit 0, 1');
if($row = mysql_fetch_assoc($rs)){
$msg = rawurlencode(iconv('gbk', 'utf-8', $row['msg']));
}
echo json_encode(array('filemtime' => filemtime('msgfile.txt'), 'msg' => $msg));
exit;
}
}
?>