Php 使用jquery设置时间间隔的Ajax
我正在处理一条通知消息,我想使用Ajax和Jquery每隔10秒从页面调用Php 使用jquery设置时间间隔的Ajax,php,jquery,ajax,Php,Jquery,Ajax,我正在处理一条通知消息,我想使用Ajax和Jquery每隔10秒从页面调用check_new-reply.php加载一条新消息,但我的代码没有显示任何内容我不知道错误是什么,有人能帮我吗 <script> $(document).ready(function(){ $(function(){ var timer = 10; var test = ""; function inTime(){ setTimeOut(inTime, 1000); $("#timer-u")
check_new-reply.php
加载一条新消息,但我的代码没有显示任何内容我不知道错误是什么,有人能帮我吗
<script>
$(document).ready(function(){
$(function(){
var timer = 10;
var test = "";
function inTime(){
setTimeOut(inTime, 1000);
$("#timer-u").html("Time refreshing"+timer);
if(timer == 8){
$("#message-u").html("Loading....");
$.POST("check_new_reply.php",{testing:test}, function(data){
$("#message-u").html(data);
})
timer = 11;
clearTimeout(inTime);
}
timer--;
}
inTime();
});
});
</script>
$(文档).ready(函数(){
$(函数(){
无功定时器=10;
var检验=”;
函数inTime(){
setTimeOut(inTime,1000);
$(“#timer-u”).html(“时间刷新”+计时器);
如果(计时器==8){
$(“#message-u”).html(“加载…”);
$.POST(“check_new_reply.php”,{testing:test},函数(数据){
$(“#message-u”).html(数据);
})
定时器=11;
clearTimeout(inTime);
}
计时器--;
}
inTime();
});
});
这里是PHP
<?php include($root . '_inc/Initialization.php');?>
<?php require_once("_inc/dbcontroller.php"); $db_handle = new DBController();?>
<?php
$users = $_SESSION['username'];
$newquery = "SELECT * FROM blog_post
INNER JOIN replys
ON blog_post.UserName = '$users'
WHERE replys.read = 0
ORDER BY rtime";
$newhisory = mysql_query($newquery);
while($newrow = mysql_fetch_array($newhisory)){
echo '<div class="fnot"><a href="/questions/postid/'.$newrow['BID'].'" id="readme">'.htmlentities($newrow['blog_title']).'</a>';
echo '<span class="ttcredit"><font color="darkgreen">94</font> </span> <a class="reqttag reqttag2" href="#">No</a> ';
echo '</div>';
echo '<input type="hidden" id="unr" name="unr" value="'.$newrow['BID'].'"/>';
}
?>
如果您只想每10秒调用一次,请在
设置超时中使用10000
毫秒。此外,最好仅在完成前一个Ajax调用时再次调用该函数:
$(document).ready(function(){
$(function(){
var test = "";
function inTime(){
$.POST("check_new_reply.php",{testing:test}, function(data){
$("#message-u").html(data);
setTimeout(inTime, 10000);
});
}
inTime();
});
});
要以一定的间隔调用任何函数,必须使用
<script>
$(document).ready(function(){
window.setInterval(function(){
myAjaxCall();
}, 10000);
});
function myAjaxCall() {
alert("Hi");
$("#message-u").html("Loading....");
$.POST("check_new_reply.php",{testing:test}, function(data){
$("#message-u").html(data);
});
}
</script>
$(文档).ready(函数(){
setInterval(函数(){
myAjaxCall();
}, 10000);
});
函数myAjaxCall(){
警报(“Hi”);
$(“#message-u”).html(“加载…”);
$.POST(“check_new_reply.php”,{testing:test},函数(数据){
$(“#message-u”).html(数据);
});
}
window.setInterval将使用上述代码每隔3秒调用您的函数,并将生成一条警报消息,
您需要做的是在函数中设置ajax代码,并使用上述方法,将3000更改为10000,您的ajax调用将每10秒进行一次,这是每10秒调用javascript函数的代码,
只要复制它并检查它,您就会有一个想法,而且我已经包括了我们讨论过的jquery
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
</body>
<script>
$(document).ready(function(){
window.setInterval(function(){
myAjaxCall();
}, 3000);
});
function myAjaxCall() {
alert("Call your Ajax here");
$("#message-u").html("Loading....");
$.POST("check_new_reply.php",{testing:test}, function(data){
$("#message-u").html(data);
});
}
</script>
$(文档).ready(函数(){
setInterval(函数(){
myAjaxCall();
}, 3000);
});
函数myAjaxCall(){
警报(“在这里调用Ajax”);
$(“#message-u”).html(“加载…”);
$.POST(“check_new_reply.php”,{testing:test},函数(数据){
$(“#message-u”).html(数据);
});
}
计时器=11上方的括号后缺少分号。另外,我认为.POST应该是小写的,将setTimeout
(注意我的大写)移出inTime
函数。请给我举个例子,说明你在说什么?仍然没有将datas@Ralphtry加载到console.log(“Ajax调用”)
在$.post回调中。如果每10秒有一个控制台,那么问题不在脚本中。它在你的PHP中。John,拜托,我是网络开发新手,你能告诉我怎么做吗?我应该在我的问题中附加我的PHP吗?是的,请添加你的PHPcheck\u new\u reply.PHP
。让我们检查一下。@Micheal我编辑了我的答案,将setTimeOut
更改为setTimeOut
我刚刚尝试过你的答案仍然是一样的。你能在代码段上显示吗?或者我需要在我的网站中包含任何jquery libery链接吗?是的,如果你使用任何jquery代码,那么你必须在顶部包含任何jquery,你加了吗?我举了一个例子,在这个例子中,我的函数每10秒被调用一次,并生成一个警报。你想要吗?只需将其复制到单个文件中,您就会有一个想法,@Pinit但不从php页面加载数据删除警报并让ajax post执行。检查您的ajax调用是否从xhr执行的firebug。