Monday 5 November 2018

conditional statements - 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(){
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...


Answer



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

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...