0

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.

6
  • I presume you're not actually doing an alert in that function? But rather some kind of [async] I/O (ajax request or whatnot)? Commented Sep 23, 2018 at 19:44
  • Unless someFunction has 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. Commented Sep 23, 2018 at 19:45
  • 2
    OP if you explain what the function actually does it will a) save everyone guessing and b) you'll get a better answer. Commented Sep 23, 2018 at 19:52
  • @Andy someFunction() does some DOM manipulation after a page has loaded. The execution time really depends on the browser, cpu speed etc. There is no AJAX involved here. Commented Sep 23, 2018 at 20:12
  • 2
    @elton73 In that case, you need to ask yourself, if you are doing anything asynchronous, or not. DOM manipulation is synchronous. Commented Sep 23, 2018 at 20:14

4 Answers 4

4

You can put your window.close() right under your alert();. However, if you would like to add more asynch stuff, you want a callback function like this.

<script type="text/javascript">
    var closeWindow = function() {
        window.close();
    };

    function someFunction(callback) {
        alert('hi');
        callback();
    }

    document.getElementById('myID').addEventListener("click", function(){
        someFunction(closeWindow);
    });
</script>
Sign up to request clarification or add additional context in comments.

6 Comments

Why would he need a callback if he does synchronous stuff only?
Is it not safer to use a callback function in case he wants to add asynch stuffs?
No, the OP does not need a callback here.
As a note: instead of var closeWindow = function() {} you should write function closeWindow() {} there is no need or benefit of using an assignment expression here. Using an expression indicates that the closeWindow variable might have a different function assigned at later point or different place.
True, however, I almost put ()=>{} as well as I like to make them learn similitude in coding.
|
2

Synchronous javascript will always be run in the top-down order and will always guarantee that instruction one will be finished before instruction two.

But, there is also an asynchronous part (you don't have it here, but for example ajax requests are often asynchronous), that is executed differently and requires callbacks/promises.

Comments

0

Your options are:

  1. put window.close(); in the callback function
  2. use promises this way you can control if the callback execution /if there is an error you can handle it better and not close the window/

Comments

0

someFunction() does some DOM manipulation after a page has loaded

Thats synchronous (the browser might defer the rendering, but were talking about milliseconds here), and therefore it is guaranteed that the DOM manipulation is done before the function returns.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.