내 유스 케이스에서는 외부 및 인라인 자바 스크립트 콘텐츠를 모두 사용하고 있습니다. 나는 다음과 같은 구조를 가지고있다.인라인 자바 스크립트 로딩 시간을 지연시키는 방법은 무엇입니까?
app/
header.html
home.html
config-load.js
footer.html
home.html
는 header.html
및 footer.html
이 포함되어 있습니다. header.html
파일에는 config-load.js
이 포함됩니다.
config-load.js
은 golang 백엔드에서 스테이지를 기반으로 configs를 가져 오기 위해 ajax 호출을합니다. 이것은 몇 밀리 초의 지연이있을 수 있습니다.
config-load.js
ajax 호출로 수집 된 구성을 사용하는 home.html
에는 인라인 스크립트가 거의 없습니다.
그래서 config-load.js
인라인 스크립트를로드하기 전에 ajax 호출을 완료해야합니다. 하지만 다른 방향으로 로딩 중입니다.
while(configReceived == false)
{
setTimeout(function(){
console.log("waiting for config");
}, 2000);
}
if(configReceived)
{
//process configs
}
그러나이 블록 스레드, 아래와 같이 인라인 스크립트로드 시간을 지연 while 루프를 사용했습니다. 페이지가 while 루프에서 멈 춥니 다. 이것을 달성 할 다른 방법이 있습니까?
편집 1 : 여기 이
<script type="text/javascript">
window.onload = function() {
time = new Date($.now());
var tagsArray = ["C", "C++", "Go", "Ruby"];
//var tagsArray = [];
requestJSON = '{"Method":"GET","AppName":"Web-app","ServiceURL":"'+endpoints.Tags.HTTPEndpoint.URL+'","Properties":null,"Object":"","Timestamp":"'+time+'"}'
$.ajax({
type: "GET",
url: endpoints.Tags.HTTPEndpoint.URL,
data: requestJSON,
processData: false,
contentType: "application/json;",
dataType: "json",
async: false,
success: function(data){
console.log("tags retrieved successfully info updated successfully")
console.log("Tags ", data.Object)
tagsArray = data.Object
},
failure: function(errMsg) {
console.log("Error occured in getting tags ", errMsg)
}
});
$("#myTags").tagit();
$("#tags").tagit({
fieldName: "tagsName", // The name of the hidden input field
availableTags: tagsArray,
allowSpaces:true,
caseSensitive:false,
removeConfirmation:true,
placeholderText:"Tags",
tagLimit: 5,
allowDuplicates: false,
singleField: true, // Use a hidden input element with the fieldName name
singleFieldDelimiter: ',', // Optional, default value is same.
onlyAvailableTags: false
});
}
</script>
그리고 내 설정 - load.js 아래처럼 보이는, 인라인 스크립트 내용입니다
//////////////////////////////////////////////////////////
// code block to get the service endpoints by stage starts
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
endpoints = JSON.parse(xhr.responseText);
console.log("server endpoints be ", endpoints);
configReceived = true;
}
}
xhr.open("GET", "/config", true);
try {
xhr.send();
} catch (err) {
// handle error
console.log("Error occured in getting the service endpoints. This may break all ajax services");
}
// code block to get the service endpoints by stage ends
////////////////////////////////////////////////////////
내가 지난 3 일 동안 노력하고 있지만, 불운.
'footer.html'에'config-load.js'와'anotherscript.js'를 넣으십시오. –