如何使用jQuery或AJAX刷新格以10秒间隔 [英] how to use jquery or ajax to refresh a div at 10 second intervals

查看:134
本文介绍了如何使用jQuery或AJAX刷新格以10秒间隔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在所有的任何帮助这里pciated乡亲AP $ P $。我建立在PHP的Web应用程序,我使用的是Yii的MVC框架里面有很多的内置工具。正如标题所说,我需要刷新一个div每10秒。目前,我有这个AJAX功能

Any help at all is appreciated here folks. I'm building a web app in php and I'm using the Yii MVC framework which has a lot of built in tools. Just as the title says, i need to refresh a div every 10 seconds. At the moment I have this ajax function

<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>

对于那些你们谁不熟悉的Yii它存储大部分的PHP文件名为保护的文件夹中。那么它就是这样,对code中的ajaxRequest.open线以上的请求URL被存储在受保护的文件夹里面,所以我不断收到禁止访问403错误。任何的想法如何,我可以实现不同的东西用jQuery或避开这个接入问题?

For those of you who are unfamiliar with Yii it stores most of your php files in a folder called protected. Well it is just that, the ajaxRequest.open line of code above is requesting that url which is stored inside the protected folder, so I keep getting an access forbidden 403 error. Any ideas how I could implement something different with jquery or get round this access problem?

推荐答案

使用设的jQuery

Usin jquery

$(function() {
    function callAjax(){
        $('#myDiv').load("http://yourdomain.com");
    }
    setInterval(callAjax, 5000 );
});

这篇关于如何使用jQuery或AJAX刷新格以10秒间隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆