Is JavaScript guaranteed to be single-threaded?

后端 未结 12 1659
遇见更好的自我
遇见更好的自我 2020-11-21 06:38

JavaScript is known to be single-threaded in all modern browser implementations, but is that specified in any standard or is it just by tradition? Is it totally safe to assu

12条回答
  •  心在旅途
    2020-11-21 06:56

    I've tried @bobince's example with a slight modifications:

    
    
        Test
    
    
        
        

    So, when you press Run, close alert popup and do a "single thread", you should see something like this:

    click begin
    click end
    result = 20, should be 20
    

    But if you try to run this in Opera or Firefox stable on Windows and minimize/maximize window with alert popup on screen, then there will be something like this:

    click begin
    resize
    click end
    result = 15, should be 20
    

    I don't want to say, that this is "multithreading", but some piece of code had executed in a wrong time with me not expecting this, and now I have a corrupted state. And better to know about this behavior.

提交回复
热议问题