javascript 等待某事成真
我在 javascript 函数中创建了一个原型窗口。在窗口中,我加载一个网站,用户必须在其中选择某些内容。我希望 javascript 函数等待用户选择某些内容,然后返回用户选择的内容的值。
function showTargetDirectoryChooser(){
var win = new Window( 'dirchooser_' + new Date().getTime() , {className: 'alphacube', width: 320, height: 470, url: '/directories/choose', maximizable: false});
win.showCenter();
win.setDestroyOnClose();
// WAIT_UNTIL( win.content.contentWindow.Directory != null )
return win.content.contentWindow.Directory
}
我发现这里我可能会使用的东西- 但我不明白如何...
i create in a javascript function a prototype window. in the window i load a site where the user has to select something. i want the javascript function to wait until the user selected something and then return the value of the what the user selected.
function showTargetDirectoryChooser(){
var win = new Window( 'dirchooser_' + new Date().getTime() , {className: 'alphacube', width: 320, height: 470, url: '/directories/choose', maximizable: false});
win.showCenter();
win.setDestroyOnClose();
// WAIT_UNTIL( win.content.contentWindow.Directory != null )
return win.content.contentWindow.Directory
}
i found here something i could maybe use - but i dont understand how to...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个异步过程;通过回调来处理这个问题可能会更好。
例如,您不能使用 closeCallback 吗?
有了这个,你就会
变成
This is an asynchronous process; it’s probably better to handle this with a callback.
For example, couldn’t you use a closeCallback?
With this, you would change
into
一种选择是使用事件处理程序。当通过单击或更改事件设置目录时,如果触发了该事件,请附加一个处理程序,该处理程序接受该事件并将其传递回主窗口中的函数。这需要使您的代码异步 - 以便您有一个调用
showTargetDirectoryChooser()
的调用者和一个将目录结果作为单独函数的回调。不过,在那里重新构建代码并将其分解给调用者和回调应该不会太复杂。您还可以使用 setTimeout 并轮询
win.content.contentWindow.Directory
的内容,如下所示:这样做还要求您的代码是异步的。
另一种选择是查看 jquery wait,但这是超时而不是等待条件。或者有一组 反应式扩展JS,但这对于你正在做的事情来说似乎有点过分了。不过,可观察量的概念可能是您想要研究的内容。
One option would be to use an event handler. When
Directory
is set via click or perhaps a change event, if that is fired, attach a handler which takes that event and passes it back to a function in your main window. This will require making your code asynchronous - so that you have a caller that callsshowTargetDirectoryChooser()
and a callback that takes the result of the directory as separate functions. It shouldn't be too complicated to re-architect your code there and break it up to the caller and the callback, though.You can also use setTimeout and poll the contents of
win.content.contentWindow.Directory
like so:Doing it this way also requires that your code be asynchronous.
Another option is to look into jquery wait, but that is a timeout as opposed to waiting on a condition. Or there is a set of reactive extensions to JS, but that looks to be overkill for what you are doing. The concept of an observable may be something you want to look into, though.