开发者

javascript WAIT for something to be true

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(开发者_JAVA百科){
  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...


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜