Javascript/Jquery代码使我的浏览器崩溃

Javascript/Jquery code crashing my browser

本文关键字:我的 浏览器 崩溃 代码 Jquery Javascript      更新时间:2023-09-26
使用

网络的东西有点新手,很抱歉,如果这是一个愚蠢的问题,但是,我似乎无法理解为什么我用来淡入和淡出引号文本的 javascript 代码使我的浏览器崩溃,代码如下。

    <!DOCTYPE html>
<html>
<head><style type="text/css">h1 { font-size: 60px; 
  font-family: 'PT Sans Caption', sans-serif;
color: #F5F5F5;
text-shadow: 0px 0px 6px rgba(255,255,255,0.7);
text-align: center;
}
html { 
  background: url(http://www.mediafire.com/convkey/9176/gipuztaktb22sw36g.jpg) no-repeat center center fixed; 
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;
}
subline {
display: run-in;
width:80%;
height:5px;
background-color:#F5F5F5;
box-shadow: 0px 0px 6px rgba(255,255,255,0.7);
position: relative;
left: 10%;
}
p {
color: #F5F5F5;
font-size: 20px; 
font-family: 'Open Sans Condensed', sans-serif;
text-align: center;
}
</style>

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
 <link href="http://fonts.googleapis.com/css?family=PT+Sans+Caption" rel="stylesheet" type="text/css">
  <link href="http://fonts.googleapis.com/css?family=Open+Sans+Condensed:300" rel="stylesheet" type="text/css">
<title>HD188753</title>
<style type="text/css"></style></head>
<body style="">
  <h1>HD188753
    <subline></subline>
  </h1>
  <p id="Quote" style="display: none; ">
    "Metaphysical  
    <b> quote </b>
    here"
  </p>

<script>
function pausecomp(ms) {
ms += new Date().getTime();
while (new Date() < ms){}
} 
x = 0;
while (x < 1)
{
$("p").fadeOut(400);
pausecomp(2000);
$("p").fadeIn(400);
pausecomp(2000);
}

</script></body>

无论如何,感谢您的任何建议或帮助。

你的while循环使 JavaScript 引擎保持 100% 的运行(因为无限循环)。 您需要查看 fadeIn/fadeOutcomplete 参数,并且需要了解 JavaScript 的setTimeout方法。

基本上,complete函数在动画完成后运行。 setTimeout是不言自明的:它在经过指定的时间量后运行指定的函数。 下面是一个示例:

function fadeOut() {
    $("p").fadeOut(400, function () {
        setTimeout(fadeIn, 2000);
    });
}
function fadeIn() {
    $("p").fadeIn(400, function () {
        setTimeout(fadeOut, 2000);
    });
}
fadeOut();
x = 0;
while (x < 1)
{
$("p").fadeOut(400);
pausecomp(2000);
$("p").fadeIn(400);
pausecomp(2000);
}

你处于一个无限循环中。x 的值没有在循环内的任何地方纵,因此它试图无限次地执行淡入和淡出,这会杀死浏览器的窗口。

我不知道

你说的"崩溃"是什么意思,但你的代码中有一个无限循环

x = 0;
while (x < 1) { ... }

这可能是您的代码永远无法完成的原因。