开发者

refresh page using jquery ajax

I have a web application where I am showing user comments on a page with its posting time. Since there are comments posted regularly so I need to update the comment posting time after every 1 minute. For this purpose I am using jquery ajax to update the time from db. My code is this:

<script type="text/javascript">

 setInterval("UpdateTime()", 60000);


function UpdateTime() {
    var id=$('#hdID').val();
    $.ajax({
        type: "POST",
        url: "mypage.aspx/UpdateTime",
        data: "{'id':'" + id + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            if (msg.d.length &gt; 0) {
                obj = document.getElementById('feed_TimeStamp' + id);
                obj.innerHTML = msg.d;
                //$(obj).slideDown("slow");
            }
        },
        async: false,
        error: function(xhr, status, error) {
            //alert(xhr.statusText);
        }
    });
}

</script>

cs file:

   [WebMethod]
   public static string UpdateTime(string id)
   {
       Comments comments = new Comments();
       string time = &quot;&quot;;
       if (comments.LoadByPrimaryKey(Convert.ToInt32(id)))
           time = MyClass.GetTime(comments.CommentsDate);
       if (!(time.ToLower().Contains(&quot;sec&quot;) || time.ToLower().Contains(&quot;min&quot;)))
           time = &quot;&quot;;
       return time;
   } 

But here my issue is after every 1 m开发者_如何学Cinute when this web Method is executed the whole webpage is hanged away until the ajax call is finished. I want to refresh the time without busing the page.


This could be a memory PROBLEM + change the settings in your ajax call for async.

from

async: false

to

async: true

when you say async: false that means you are not allowing the browser to send your request in Asynchronous way which is quite opposite to AJAX - it should be an asynchronous call.

See the documentation here:

async Default: true By default, all requests are sent asynchronously (i.e. this is set to true by default). If you need synchronous requests, set this option to false. Cross-domain requests and dataType: "jsonp" requests do not support synchronous operation. Note that synchronous requests may temporarily lock the browser, disabling any actions while the request is active.

Suggestion:

I think you are trying to load all comments which could be a LOT in memory if your result set is high for browser to keep the data and post it on the element.

I would suggest you to request only new data from the server instead of whole "which is not on the comments panel", that way you are not bugging browser to keep everything in the memory.


When you use the jquery ajax function with the

async: false,

the page will remain busied until the request ends.


Take this out: async: false (by default it is true)

You should not make ajax call in synchronusly unless extreme rare case where you must wait.

This will definitely lock the page from functioning (not even a mouse click possible)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜