boo-urns
boo-urns

Reputation: 10376

Race condition in JavaScript with onload event

Let's say I'm loading a page that contains this code:

<script>
    function functionAlert() {
        alert(window.myProperty);
    }

    function functionSetProperty() {
        window.myProperty = "hello!";
    }
    window.addEventListener('load', functionAlert, false);
    functionSetProperty();
</script>

Is the race condition possible that functionAlert gets called before functionSetProperty, in which case the alert will show undefined?

Upvotes: 0

Views: 788

Answers (1)

cdhowie
cdhowie

Reputation: 168958

No, this is not possible. Browser-based JavaScript is single-threaded and blocks the browser from processing events while it is executing.

(I've seen some exceptions to this in the wild, but they've all centered around XmlHttpRequest.)

Upvotes: 3

Related Questions