Rebooting Div with jquery timer

I am trying to update a div on a page with the name of the server on which the page is displayed. This is just for the test, so we can check what happens when the cookie’s battery life expires. I want to do this with jquery, but I can't get anything to work. I found an online example that I worked on, but keep getting errors.

We have an html page on each of our servers that simply contains the server name. This is the text from this page that we want to display in the div.

Below is the code I found. Can anybody help me? I am new to jquery. Any help would be greatly appreciated.

Thank,

`          

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

`

+3
source share
2 answers

Try the following:

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

So what is going on?

refresh , , ajax- path/to/server/page.php, .

div#server-name, a setTimeout, refresh interval .

+9

,

, . MVC ASP.NET WebForms?

<html>
<head>
    <script type="text/javascript" src="jquery-1.5.1.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            var timer = setInterval( updateDiv, 5000);
            var counter = 0;  //only here so we can see that the div is being updated if the same error is thrown

            function updateDiv() {
                var messageDiv = $('#content');
                $.ajax({
                        type: 'GET',
                        async: false,
                        url: "your.site.com/path/page.ext",
                        cache: false,
                        success: function(result) {
                            counter++;
                            messageDiv.empty();
                            messageDiv.append(result);
                            messageDiv.append("<br />");
                            messageDiv.append("counter = " + counter);
                        },
                        error: function(xhr, ajaxOptions, thrownError) {
                            counter++;
                            messageDiv.empty();
                            messageDiv.append("thrown error: " + thrownError);
                            messageDiv.append("<br />");
                            messageDiv.append("status text: " + xhr.statusText);
                            messageDiv.append("<br />");
                            messageDiv.append("counter = " + counter);
                        }
                });
            }   

        });
    </script>
</head>
<body>
    <h2>Div is below here</h2>
    <div id="content"></div>
</body>
</html>
0

All Articles