It's possible this may vary by browser, but probably not. The consensus (see below) seems to be that the script seems to complete, or is at least given several seconds to complete; once it has (or after some period of time, anyway), the browser closes the tab.
You can fairly easily test this on modern browsers by doing a busy-wait and seeing what happens when you try to close the browser during the busy-wait.
For instance, consider this page:
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Example</title>
<style>
html, body {
margin: 0;
padding: 0;
}
</style>
<body>
<p>I'm here</p>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
var d = Date.now() + 10000;
while (Date.now() < d) {
}
</script>
</body>
</html>
Chrome: Shows "I'm here" and if you click the close tab button, it doesn't close for several seconds (seems like the loop completes). (If it never finished, odds are Chrome would eventually tell you the script was misbehaving.)
Firefox: Doesn't show "I'm here" but the UI does the same thing: Doesn't close the tab for several seconds after you request the tab closure.
IE11: May or may not show "I'm here", clicking the "close tab" button seems to respond more quickly (but still after several seconds) than Chrome.
It seems like it's fairly easy to go the click in before the script starts, so you do have to wait a moment after the page loads before clicking the close button.