Javascript:等待条件为真的非阻塞方式 [英] Javascript: Non-blocking way to wait until a condition is true

查看:32
本文介绍了Javascript:等待条件为真的非阻塞方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个 ASP.NET UpdatePanels,每个都有一个 AsyncPostBackTrigger 绑定到同一个按钮的服务器端点击事件.由于一次只有一个 UpdatePanel 可以做它的事情,我使用 .get_isInAsyncPostBack()PageRequestManager 来防止用户访问页面的另一部分直到异步回发完成.

I have several ASP.NET UpdatePanels, each with an AsyncPostBackTrigger tied to the same button's serverside click event. Since only one UpdatePanel can be doing its thing at a time, I use .get_isInAsyncPostBack() of the PageRequestManager to prevent a user from being able to access another part of the page until the async postback is complete.

此页面的另一部分需要连续动态更新多个更新面板.由于更新面板使用异步触发器,调用 __doPostBack("<%=ButtonName.ClientID %>", 'PanelId'); 会异步触发.因此,它将快速移动到循环的下一次迭代并尝试更新下一个面板.但是,第二次迭代失败,因为已经有另一个更新面板在执行异步回发.

Another part of this page needs to dynamically update multiple update panels consecutively. Since the update panels use async triggers, calling __doPostBack("<%=ButtonName.ClientID %>", 'PanelId'); fires asynchonously. Because of this, it will quickly move along to the next iteration of the loop and try to update the next panel. However, the second iteration fails because there is already another update panel doing an async postback.

理想情况下,有一种方法可以等到 .get_isInAsyncPostBack() 返回 false 而不会阻止其他客户端活动.

Ideally, there would be a way to wait until .get_isInAsyncPostBack() returns false without blocking other client activity.

研究使我发现很多人遇到了我的问题,几乎所有人都被建议使用 setTimeOut().我不认为这对我有用.我不想在执行函数之前等待指定的时间.我只是希望我的 Javascript 在另一个脚本运行时等待,最好等到特定条件为真.

Research has lead me to a lot people with my problem, almost all of whom are advised to use setTimeOut(). I do not thing this will work for me. I don't want to wait for a specified amount of time before executing a function. I simply want my Javascript to wait while another script is running, preferably wait until a specific condition is true.

我知道很多人可能会建议我重新考虑我的模型.这实际上不是我的模型,而是交给我们的开发团队的模型,该模型目前在幕后一团糟.由于时间限制,重写模型不是一种选择.唯一的选择是使这项工作.我想如果我有办法让客户端代码在不阻塞的情况下等待,我的问题就解决了.

I understand that many will probably want to suggest that I rethink my model. It's actually not my model, but one that was handed to our development team that is currently a total mess under the hood. Due to time contraints, rewriting the model is not an option. The only option is to make this work. I think that if I had a way to make the client code wait without blocking, my problem would be solved.

推荐答案

javascript 中没有诸如等待或睡眠之类的功能,因为它会阻止浏览器响应.

There is no such functionality such as wait or sleep in javascript, since it would stop browser from responding.

在您的情况下,我会采用类似于以下内容的方法:

In your case I would go with something similar to following:

function wait(){
  if (!condition){
    setTimeout(wait,100);
  } else {
    // CODE GOES IN HERE
  }
}

这篇关于Javascript:等待条件为真的非阻塞方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆