Click event not fireing when page is inactive for some time
好的,这是我的问题:)。
我的代码后面有几个[WebMethods],并使用jquery ajax从服务器获取数据。
然后它发生:)。过了一段时间,当页面处于非活动状态时,当我尝试单击应该向服务器发送请求的按钮时,它仅在约半分钟内什么都不做,只有事件被触发,我才能从服务器得到响应。
我的JavaScript看起来像这样:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | addToCart.click(function () { AddOrRemoveItemToCart($(this)); }); function AddOrRemoveItemToCart(control) { var itemId = contol.attr("id"); $('document').ready(function () { $.ajax({ type:"POST", url:"Home.aspx/AddOrRemoveItemToCart", data:"{itemId:" + itemId +"}", contentType:"application/json; charset=utf-8", dataType:"json", success: function (data) { if (data.d.length > 0) { SucessAddItemToCart(data.d); } }, error: function (textStatus) { alert(textStatus); } }); }); } function SucessAddItemToCart(data) { //DO SOMETHING WITH DATA } |
我的服务器端代码如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | [WebMethod] public static List<CartItem> AddOrRemoveItemToCart(string itemId) { List<CartItem> items = new List<CartItem>(); List<CartItem>temp = new List<CartItem>(); bool check = false; if(HttpContext.Current.Session["items"]!=null) { items = (List<CartItem>)HttpContext.Current.Session["items"]; foreach(CartItem c in items) { if(c.Id != itemId) temp.Add(c); else check = true; } if(!check) temp.Add(new CartItem{Id = itemId}); } HttpContext.Current.Session["items"]=temp; return temp; } |
通常我会说您的会话已过期。但是由于该事件在半分钟后触发,因此必须是其他事件。
尝试使用Firebug进行调试,并检查AddOrRemoveItemToCart是否立即被调用。您还可以查看浏览器和服务器之间的流量。
您的事件应立即触发,但正如雷米(Remy)所说,您的会话可能已过期,因此需要一些时间来重新建立会话对象并处理请求。