javascript 等待某事成真

发布于 2024-10-27 03:31:35 字数 640 浏览 3 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一身骄傲 2024-11-03 03:31:35

这是一个异步过程;通过回调来处理这个问题可能会更好。

例如,您不能使用 closeCallback 吗?

function showTargetDirectoryChooser(done){
  var win = new Window( 'dirchooser_' + new Date().getTime() , {className: 'alphacube', width: 320, height: 470, url: '/directories/choose', maximizable: false});
  win.showCenter();
  win.setDestroyOnClose();

  // This will ensure 
  win.setCloseCallback(function () {
      done(win.content.contentWindow.Directory);
      return true; // or return false if you don't want the window to be closed
  });

  return true;
}

有了这个,你就会

var chosenDir = showTargetDirectoryChooser();
// do something with chosen directory

变成

var chosenDir;
showTargetDirectoryChooser(function (directory) {
    chosenDir = directory;
    // do something with the chosen directory
});

This is an asynchronous process; it’s probably better to handle this with a callback.

For example, couldn’t you use a closeCallback?

function showTargetDirectoryChooser(done){
  var win = new Window( 'dirchooser_' + new Date().getTime() , {className: 'alphacube', width: 320, height: 470, url: '/directories/choose', maximizable: false});
  win.showCenter();
  win.setDestroyOnClose();

  // This will ensure 
  win.setCloseCallback(function () {
      done(win.content.contentWindow.Directory);
      return true; // or return false if you don't want the window to be closed
  });

  return true;
}

With this, you would change

var chosenDir = showTargetDirectoryChooser();
// do something with chosen directory

into

var chosenDir;
showTargetDirectoryChooser(function (directory) {
    chosenDir = directory;
    // do something with the chosen directory
});
写下不归期 2024-11-03 03:31:35

一种选择是使用事件处理程序。当通过单击或更改事件设置目录时,如果触发了该事件,请附加一个处理程序,该处理程序接受该事件并将其传递回主窗口中的函数。这需要使您的代码异步 - 以便您有一个调用 showTargetDirectoryChooser() 的调用者和一个将目录结果作为单独函数的回调。不过,在那里重新构建代码并将其分解给调用者和回调应该不会太复杂。

您还可以使用 setTimeout 并轮询 win.content.contentWindow.Directory 的内容,如下所示:

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();

  setTimeout("pollDirectory()", 500); // poll in a half second
}

function pollDirectory() {
  if(win.content.contentWindow.Directory != null) {
    callback(win.content.contentWindow.Directory); // you will need an asynch callback
  } else {
    setTimeout("pollDirectory()", 500); // poll in a half second
  }

}

这样做还要求您的代码是异步的。

另一种选择是查看 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 calls showTargetDirectoryChooser() 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:

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();

  setTimeout("pollDirectory()", 500); // poll in a half second
}

function pollDirectory() {
  if(win.content.contentWindow.Directory != null) {
    callback(win.content.contentWindow.Directory); // you will need an asynch callback
  } else {
    setTimeout("pollDirectory()", 500); // poll in a half second
  }

}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文