This might not be a complicated question but I'm not sure if I'm on the right track here.
My goal is to run a JavaScript function first and when the execution is completely finished (this may take up to a few seconds, but not always) close the window.
The function and closing is triggered by just 1 button:
<button id="myID">Click me</button>
The JavaScript is:
<script type="text/javascript">
function someFunction() {
// do some stuff
}
document.getElementById('myID').addEventListener("click", function(){
someFunction();
window.close();
});
</script>
This works fine, but can get anyone give some advice if this is a reliable method or not? It is important that execution of the function is fully completed before window.close() is triggered.
alertin that function? But rather some kind of [async] I/O (ajax request or whatnot)?someFunctionhas code expecting asynchronous results, such as an ajax request, and you want those completed before closing the window, then yes, the rest of the code is guaranteed to execute after it.