When does InnerHTML execute immediately?

后端 未结 1 769
轮回少年
轮回少年 2021-01-28 04:04

I would expect this HTA code to print a to be replaced some time later by b. However, what it does is it prints nothing for some time, and then b

1条回答
  •  别那么骄傲
    2021-01-28 04:48

    Scripts and rendering are executed in a single thread. This means, that doThis will be finished before rendering starts. In the meanwhile you've set the inner HTML to 'b', and that will be the only result you'll see.

    To fix this, you've to finish the first sub, and call intensiveOperation with a short delay, and make the final HTML update at the end of the second sub, like so:

    Sub doThis
        statusinfo.InnerHTML = "a"
        Call window.setTimeout(GetRef("intensiveOperation"), 4)
    End Sub
    
    Sub intensiveOperation()
        'takes long
        statusinfo.InnerHTML = "b"
    End Sub
    

    Finishing doThis will free the thread. Notice, that setTimeout won't halt the execution, the whole sub is executed to the end. After that browser can render the page.

    intensiveOperation is called at some point after the rendering is finished, not earlier than the given delay, but possibly much later, depending on tasks CPU is doing at the time.

    The answer to the question in the header of the question is basically: never.

    0 讨论(0)
提交回复
热议问题