javascript - HTML 网站在执行 JavaScript 时崩溃

标签 javascript html crash

每当我加载位于 here 的网站时,屏幕几乎全黑并崩溃。与此同时,链接服务器页面也会崩溃。我可以获得一些帮助来查找错误吗?我检查了多个验证站点,没有一个显示错误。

这是代码:

<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>

最佳答案

你有一个无休止的递归。您无条件地从自身调用 update()

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

关于javascript - HTML 网站在执行 JavaScript 时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30040756/

相关文章:

java - 我的Android应用程序在某些设备上崩溃到java.lang.RuntimeException吗?

javascript - Highcharts Arareange 超过一定长度没有缩放动画

javascript - 回流 Action 触发两次

javascript - NodeJS 对数组中的每个对象进行异步处理

javascript - 从 Javascript 创建视频时无法应用 video-js 皮肤

javascript - 将超链接应用于整个 DIV 区域?

c - OS X 10.5 malloc 中的错误?

javascript - 如何从组件外部访问路由参数?

html - 使用 CSS 来格式化日期/时间或百分比值?

iOS 应用程序在闲置一段时间后崩溃