2017-02-10 37 views
1

this SO question和其他許多類似規定中所述,頁面上執行<script>的順序應與html文檔中定義這些標籤的順序相同。JavaScript - 執行順序<script>標籤

我創建了一個簡單的Java(服務器端)的測試應用程序,允許執行的請求,並且(在這個問題的底部有關的代碼片段)返回響應之前等待指定時間內。它有一個簡單的API:

http://localhost:8080/latency?time=XXX&response=YYY 

實例的請求,將在一秒鐘後返回console.log('done')(1000毫秒):

http://localhost:8080/latency?time=1000&response=console.log(%27done%27) 

接下來,我創建了一個簡單的index.html頁(nginx的服務):

<!DOCTYPE html> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
     <title>test order</title> 
    </head> 
    <body> 
     <script type="text/javascript" async=false src="http://localhost:8080/latency?time=1000&amp;response=console.log(%27done1%27)"></script> 
     <script type="text/javascript" async=false src="http://localhost:8080/latency?time=100&amp;response=console.log(%27done2%27)"></script> 
     <script type="text/javascript" async=false src="http://localhost:8080/latency?time=10&amp;response=console.log(%27done3%27)"></script> 
     <script>console.log('static script without "src" attr');</script> 
    </body> 
</html> 

,根據我看到的一切使我至今預計控制檯輸出的順序是:

done1 
done2 
done3 
static script without "src" attr 

這是我得到了(火狐51開發者控制檯):

latency requests 1

latency requests 2

這只是我期望得到相反的順序。我錯過了什麼嗎? 是否有一種方法可以按照所需的順序執行這些腳本(即按照它們在HTML中定義的順序)?

作爲參考,在服務器側的爪哇部分:

private String latency(HttpServletRequest request) { 

    long millis = Long.parseLong(request.getParameter("time")); 
    String response = request.getParameter("response"); 

    try { 
     Thread.sleep(millis); 
     return (response != null) ? response : ""; 
    } catch (InterruptedException e) { 
     throw new RuntimeException(e); 
    } 
} 
+0

'異步=「假」'(帶引號),有何幫助? – haxxxton

+1

或者完全刪除'async' –

+0

'async =「false」'使它異步。刪除這些屬性。 – nicovank

回答

5

async布爾屬性。它的價值並不重要。刪除該屬性。

如果你把