English 中文(简体)
执行只有最后一个被调用的函数
原标题:Executing only the last function called

This is the setup in short. I have a big table. Everytime a cell get focus an asyncron call to the server is done (with PageMethods) and some data is returned and updates a infobox in the page. I have written code that makes it possible to navigate between cells with the arrow keyes. The problem occurs when I shift focus fast through several cells in order to get to the cell I want. Every get-focus is executed and since the communication with the server takes about half a second it get quite irritating and not very user friendly. Ideally I would like to execute only the last focus event. But how? I cant know which event is the last one, can I? Ideas, anybody?

问题回答

保罗说得对。

要在网络上执行此操作,您需要将当前的聚焦事件与javascript时间函数 SetTimeout和ClearTimeout结合使用:

var timer; // timer variable in scope for other functions

var onFocus = function() {
  clearTimeout(timer);
  timer = setTimeout("makePageMethodCall()", 500);
}

var makePageMethodCall = function() {
  // make ajax call to your PageMethod function
}

调用setTimeout()会在调用请求服务器方法之前创建半秒的等待时间。如果在定时器到期之前发生另一个焦点事件,则clearTimeout()调用将在启动另一个定时器之前取消当前定时器。正如Paolo建议的那样,您可以尝试使用500毫秒的时间长度,以找到最适合您情况的值。

原则上,当焦点事件被触发时,您需要一个短时间计时器开始倒计时。如果在计时器到期之前发生另一个事件,则重新启动计时器;否则,对于最后一个单元格执行OnFocus逻辑。

关键是要使它足够简短以保持UI的响应性,但又足够长避免触发多次对服务器的调用。

遗憾的是,我不知道你如何为协会工作。 NET,但我已就类似情况在Winforms做了工作。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签