如何使用jquery或ajax以10秒的间隔刷新div
这里的人们非常感谢任何帮助。我正在用php构建一个web应用程序,我正在使用Yii MVC框架,它有很多内置工具。正如标题所说,我需要每10秒刷新一个div。目前我有这个ajax函数如何使用jquery或ajax以10秒的间隔刷新div,jquery,html,ajax,refresh,Jquery,Html,Ajax,Refresh,这里的人们非常感谢任何帮助。我正在用php构建一个web应用程序,我正在使用Yii MVC框架,它有很多内置工具。正如标题所说,我需要每10秒刷新一个div。目前我有这个ajax函数 <script type="text/javascript"> function ajaxFunction(){ var ajaxRequest; try{ // Opera 8.0+, Firefox, Safari ajaxRequest
<script type="text/javascript">
function ajaxFunction(){
var ajaxRequest;
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
var list = document.getElementById('logged_in_users_list');
if(ajaxRequest.readyState == 4){
list.innerHTML = ajaxRequest.responseText;
setTimeout('ajaxFunction()',10000);
}
}
ajaxRequest.open("GET", "protected/controllers/room/openRoom", true);
ajaxRequest.send(null);
}
</script>
<script type="text/javascript">
setInterval(function() {ajaxFunction();}, 5000);
</script>
函数ajaxFunction(){
var ajaxRequest;
试一试{
//Opera 8.0+、Firefox、Safari
ajaxRequest=新的XMLHttpRequest();
}捕获(e){
//Internet Explorer浏览器
试一试{
ajaxRequest=newActiveXObject(“Msxml2.XMLHTTP”);
}捕获(e){
试一试{
ajaxRequest=新的ActiveXObject(“Microsoft.XMLHTTP”);
}捕获(e){
//出了点问题
警告(“你的浏览器坏了!”);
返回false;
}
}
}
//创建一个函数,该函数将接收从服务器发送的数据
ajaxRequest.onreadystatechange=函数(){
var list=document.getElementById('logged_in_users_list');
if(ajaxRequest.readyState==4){
list.innerHTML=ajaxRequest.responseText;
setTimeout('ajaxFunction()',10000);
}
}
打开(“获取”,“受保护/控制器/房间/开放房间”,true);
ajaxRequest.send(空);
}
setInterval(function(){ajaxFunction();},5000);
对于那些不熟悉Yii的人来说,它将大部分php文件存储在一个名为protected的文件夹中。只是,上面的ajaxRequest.open代码行正在请求存储在受保护文件夹中的url,所以我一直收到一个访问禁止403错误。有什么想法可以让我用jquery实现一些不同的东西,或者绕过这个访问问题吗?与jquery中的代码大致相当的是:
//execute call immediately
(function check(){
//a GET AJAX call
$.get('protected/controllers/room/openRoom')
.done(function(data){
//when we receive, populate
$('#logged_in_users_list').html(data);
})
.always(function(){
//regardless of a fail or success, call again after 10 seconds
setTimeout(check,10000);
});
}());
403永远是403。这段代码告诉您,在jquery中不允许您输入该位置(可能您需要身份验证?
$(function() {
function callAjax(){
$('#myDiv').load("http://yourdomain.com");
}
setInterval(callAjax, 5000 );
});
//zisu.php
var auto_refresh=setInterval(
函数()
{
$('#div1').load('time.php');
}, 10000);
//time.php
// zisu.php
<html>
<head>
<script type="text/javascript">
var auto_refresh = setInterval(
function ()
{
$('#div1').load('time.php');
}, 10000);
</script>
</head>
<body>
<div id ="div1">
<?php
echo date("h:i:s A");
?>
</div>
</body>
</html>
// time.php
<?php
echo date("h:i:s A");
?>