使用jQuery Ajax刷新页面 [英] refresh page using jquery ajax
问题描述
我有一个Web应用程序,该应用程序在页面上显示用户评论及其发布时间.由于定期发布评论,因此我需要每1分钟更新一次评论发布时间.为此,我正在使用jquery ajax从db更新时间.我的代码是这样的:
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:
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文件:
cs file:
[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;
}
但是在这里,我的问题是每执行此Web方法1分钟后,整个网页就会被挂起,直到ajax调用结束.我想刷新时间而不忙页面.
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.
推荐答案
('
('#hdID').val();
.ajax({ 类型:" , url:" , 数据:" + id + " , contentType:" , dataType:" , 成功:功能(msg){ 如果(msg.d.length> 0 ){ obj = 文档 .getElementById(' feed_TimeStamp' + ID); obj.innerHTML = msg.d; //
.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"); 跨度> } }, 异步:错误, 错误:功能(xhr,状态,错误){ // 警报(xhr.statusText); } }); }
(obj).slideDown("slow"); } }, async: false, error: function(xhr, status, error) { //alert(xhr.statusText); } }); }
CS文件:
cs file:
[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;
}
但是在这里,我的问题是每执行此Web方法1分钟后,整个网页就会被挂起,直到ajax调用结束.我想刷新时间而不忙页面.
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.
这篇关于使用jQuery Ajax刷新页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!