Click here to Skip to main content
15,900,973 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
hi,
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:
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 > 0) {
                obj = document.getElementById('feed_TimeStamp' + id);
                obj.innerHTML = msg.d;
                //$(obj).slideDown("slow");
            }
        },
        async: false,
        error: function(xhr, status, error) {
            //alert(xhr.statusText);
        }
    });
}

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

But here my issue is after every 1 minute 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.
Posted
Updated 16-Jun-11 22:49pm
v2

1 solution

Look at your UpdateTime() method, you have put async: false

So you're using AJAX (asynchronous JavaScript), but without the asynchronous part!! That's why your web page is hanging, you're making a blocking call on the page.

Remove async: false and try again. Your code will run and the success or error functions will execute on completion of the web service call
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900