Javascript execution order with setTimeout()

前端 未结 1 1532
花落未央
花落未央 2021-02-20 17:11

Say that I have the following code:

function testA {
   setTimeout(\'testB()\', 1000);
   doLong();
}

function testB {
   doSomething();
}

function doLong() {
         


        
1条回答
  •  攒了一身酷
    2021-02-20 17:41

    The first of your guesses is the correct one: testB() is queued up to execute after doLong() and anything else it called have finished.

    If it takes more than one second for testA to finish, testB will simply have to wait.

    Also, you should write setTimeout(testB, 1000) rather than setTimeout('testB()', 1000). Sending a string to setTimeout is, like using eval, generally considered evil and will make you enemies ;)

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