2015-05-04 113 views
0

每當我加載位於here的網站時,屏幕變成幾乎全黑和崩潰。與此同時,鏈接的服務器頁面也崩潰。我能找到幫助找到錯誤嗎?我檢查了多個驗證網站,並沒有一個顯示錯誤。HTML網站在JavaScript執行時崩潰

下面是代碼:

<html> 
<head> 
<style> 
.titletext{ 
color:white; 
display:block; 
position:absolute; 
font-size:50px; 
width:1000px; 
margin-left:150px; 
margin-right:200px; 
} 
.nametext{ 
color:white; 
display:block; 
position:absolute; 
font-size:30px; 
width:600px; 
margin-left:500px; 
margin-right:200px; 
margin-top:600px; 
} 
.earthphoto{ 
display:block; 
position:absolute; 
margin-left:400px; 
margin-top:150px; 
} 
</style> 
</head> 
<body onload="update()"> 
<script type="text/javascript"> 
document.body.style.background="black"; 
var changescene=function(){ 
var allvariables=Object.keys(window); 
if(page===1){ 
} 
page++; 
}; 
var page=1; 
var x=0; 
var update=function(){ 
if(page===1){ 
document.body.innerHTML=""; 
var text=document.createElement("p"); 
var textclass = document.createAttribute("class"); 
textclass.value="titletext"; 
text.setAttributeNode(textclass); 
text.appendChild(document.createTextNode("Welcome to Mikey's Google Earth Presentation!")); 
document.body.appendChild(text); 
var text2=document.createElement("p"); 
text2class=document.createAttribute("class"); 
text2class.value="nametext"; 
text2.setAttributeNode(text2class); 
text2.appendChild(document.createTextNode("By Mikey Richards")); 
document.body.appendChild(text2); 
googleearthimage=document.createElement("img"); 
googleearthimage.setAttribute("src","EARTH.png"); 
googleearthimage.setAttribute("class","earthphoto"); 
document.body.appendChild(googleearthimage); 
var music=document.createElement("audio"); 
var musiclink=document.createElement("source"); 
musiclink.src="Test.mp3"; 
music.appendChild(musiclink); 
var musicclass=document.createAttribute("id"); 
musicclass.value="sound1"; 
music.setAttributeNode(musicclass); 
document.body.appendChild(music); 
if(x===0){ 
document.getElementById("sound1").play(); 
x++; 
} 
if(document.getElementById("sound1").duration===document.getElementById("sound1").currentTime){ 
changescene(); 
} 
} 
else if(page===2){ 
document.body.innerHTML=""; 
} 
update(); 
} 
</script> 
</body> 
</html> 

回答

1

你有一個無限遞歸。你自己無條件地致電update()

var update=function(){ 
    if(page===1) { 
    // your code 
    } 
    else if(page===2){ 
     document.body.innerHTML=""; 
    } 
    update(); 
} 
+0

問題是我想不斷調用更新函數。通過這種方式,它可以像setInterval()一樣工作,使其只在所有內容運行時重複。這樣,我就可以播放我在代碼中創建的音頻元素,而不必每隔幾秒就重新啓動它,這樣我就可以在音頻結尾更改頁面變量。我該如何做到這一點,以便我可以播放音頻直到結束,然後在最後更改頁面? – Mikey

+0

大規模遞歸耗盡堆棧(如果沒有其他)。你能把它當成一個循環嗎? –

+0

好的。我想出了我應該如何去做。它現在所做的就是爲身體運行更新添加onLoad,更新運行播放第一組音樂的功能,音樂具有在最後運行更新腳本的事件監聽器,更新腳本運行更新功能等等。 – Mikey

1

首先,黑屏是最有可能是由於你的腳本標籤的第一行:

document.body.style.background="black"; 

至於實際的頁面崩潰的功能「更新」不休自稱,引起無限遞歸。