用jquery定时器重新加载一个div [英] Reload a Div with jquery timer

查看:100
本文介绍了用jquery定时器重新加载一个div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用显示该页面的服务器名称刷新页面上的div.这仅用于测试,因此我们可以测试auth cookie超时时发生的情况.我想用jQuery做到这一点,但我似乎什么也没用.我在网上找到了一个我一直在努力的示例,但与此同时也不断出错.

I am trying to refresh a div on a page with the server name the page is being displayed from. This is just for test so we can test what happens when a auth cookie times out. I want to do this with jquery but I can't seem to get anything working. I found an example online that I have been working on but keep getting errors with it as well.

我们在每个服务器上都有一个html页面,其中仅包含服务器名称.这是我们要在div中显示的该页面中的文本.

We have an html page on each of our servers that just contains the server name. It is the text from that page that we want displayed in the div.

下面是我找到的代码示例.有人能帮我吗?我对jquery很陌生.任何帮助将不胜感激.

Below is the code sample that I found. Can someone help me? I'm pretty new to jquery. Any help would be greatly appreciated.

谢谢

`

`

<script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery.ui/1.8.5/jquery-ui.min.js"></script>
<script src="../Scripts/jquery-1.2.6.min.js" type="text/javascript"></script>
<script src="../Scripts/jquery.timers-1.0.0.js" type="text/javascript"></script>

<script type="text/javascript">
    $(document).ready(function () {
        var j = jQuery.noConflict();
        j(document).ready(function () {
            j(".refreshMe").everyTime(5000, function (i) {
                j.ajax({
                    url: "refresh.html",
                    cache: false,
                    success: function (html) {
                        j(".refreshMe").html(html);
                    }
                })
            })
        });
        j('.refreshMe').css({ color: "red" });
    });
</script>

`

推荐答案

尝试一下:

$(document).ready(function () {
    var interval = 500;   //number of mili seconds between each call
    var refresh = function() {
        $.ajax({
            url: "path/to/server/page.php",
            cache: false,
            success: function(html) {
                $('#server-name').html(html);
                setTimeout(function() {
                    refresh();
                }, interval);
            }
        });
    };
    refresh();
});

<div id="server-name"></div>

那是怎么回事?

页面准备好后,我将调用refresh,然后对path/to/server/page.php进行ajax调用,这将返回所需的结果.

I will call refresh once the page is ready, then an ajax call is made to path/to/server/page.php which will return the desired result.

我在div#server-name中显示结果,然后执行setTimeout,每interval毫秒将再次调用refresh.

I show the results in div#server-name then I do a setTimeout which will call refresh again every interval mili seconds.

这篇关于用jquery定时器重新加载一个div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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