【问题标题】:How do you stop browser from 'hourglassing' when Javascript is DONE (stop throbber)?当 Javascript 完成时,如何阻止浏览器“沙漏”(停止跳动)?
【发布时间】:2011-05-20 05:54:12
【问题描述】:

编写一个包含一些非常简单的 Javascript 的小型 HTML 网页,我注意到完成后,它一直在旋转(firefox)。我在其他页面上看到过很多次,一直以为是Javascript错误或循环问题,但我运行的这个程序肯定已经完成了。

这是一个小型网页的例子。在您单击按钮并处理后,悸动者(感谢 Kooilinc 的术语)继续运行。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="content-type" content="text/html;charset=utf-8" />
    <title>Grade Tester</title>
    <script language="javascript" type="text/javascript">       
      function calculate()
      {
       var grade = document.getElementById("grade").value.toUpperCase();        
        switch (grade)
        {
         case "A":
           document.write("Outstanding Achievement");
           break;

         case "B":
           document.write("Above Average");
           break;

         case "C":
           document.write("Average Achievement");
           break;

          case "D":
            document.write("Low Passing Grade");
           break;

         case "F":
           document.write("Failing Grade");
            break;
        }
      }
    </script>       
    </head>
  <body>
    Grade:<input type="text" id="grade" />
    <br /><input type="button" value="Get Result" onclick="calculate()" />
  </body>
</html>

我可以通过点击停止(右键单击并停止)来停止它。

脚本完成后,我需要做什么才能让 Javascript 停止处理(或让浏览器停止处理)?

注意 1:这发生在 FF4.01 中,而不是在 IE 或 Chrome 中(对于此代码示例)。

注意2:我在switch语句后尝试了window.stop()函数,并没有停止跳动。

根据 Lekensteyn 的回答,switch 语句后的 document.close() 解决了这个问题,但这是一个根本没有跳动并且不使用 document.close() 的示例。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="content-type" content="text/html;charset=utf-8" />
    <title>Lab 9.1</title>
  </head>
  <body>
    <script language="javascript" type="text/javascript">
      var phrase = "Every good boy does fine goes on the line."
      document.write(phrase.length + "<br />");
      for(var i = 0; i < phrase.length; i++)
      {
        document.write(phrase.charCodeAt(i) + " ");
      }
        document.write("<br />");
        document.write(phrase.toLowerCase() + "<br />");
        document.write(phrase.toUpperCase() + "<br />");
    </script>
  </body>
</html>

【问题讨论】:

  • @Dr.Molle,这就是为什么我在这个问题上如此具体地说明它有多简单,这样人们就可以在逻辑上假设最小的 javascript,然后回答这个问题。我不在电脑旁有代码,也不能给出例子,反正也没关系。连班上的老师都说javascript做完后浏览器一直跑是常事,但他也不知道怎么停止。
  • 你是否也只有 firefox 或其他浏览器有问题
  • 您的问题不具体,到目前为止您要求的是水晶球。如果您使用方法 document.open() 而不在最后关闭打开的文档,则大多数描述的行为都会出现,但这只能是猜测,不知道任何细节。
  • 有一个函数 window.stop() 在这里看到它java2s.com/Tutorial/JavaScript/0380__Window/windowstop.htm。但不确定它应该放在哪里,如果您希望必须加载页面的所有内容。
  • @Lance Roberts:这里有一些代码给你:document.close() document.write() 在文档加载后执行会打开一个文档,所以在 write() 之后需要在这个文档上使用 close() .请参阅我 10 小时前的评论。另请参阅:developer.mozilla.org/en/DOM/document.write 如果您在开始时发布了代码,则此问题已在 2 或 3 分钟后得到解决。

标签: javascript browser throbber


【解决方案1】:

调用document.write()后,如果文档之前没有关闭,则必须调用document.close()

基本上是这样的:

  1. 正在解析页面
  2. 脚本已处理
  3. 页面已“关闭”(document.close())

如果document.write() 在步骤 2 中被调用,它将在步骤 3 中完成。 在第 3 步之后,如果您调用 document.write,您将再次打开该页面,但该页面不会在 FF 中自行关闭(不确定其他人)。你必须通过调用document.close()来关闭它。

当我需要在 FF 中打开一个窗口并快速写入时,我发现了这一点。

var win = window.open();
win.document.write("something");
// the next line is required to finish the page
win.document.close();

这同样适用于您的情况:

<script type="text/javascript">
function calculate()
{
    var grade = document.getElementById("grade").value.toUpperCase();
    switch (grade)
    {
    case "A":
        document.write("Outstanding Achievement");
        break;
    // removed some cases
    case "F":
        document.write("Failing Grade");
        break;
    }
    // finish the document
    document.close();
}
</script>

【讨论】:

  • 好的,但是我有其他使用 document.write() 的示例没有 document.close() 并且仍然没有跳动(尽管它确实解决了这个示例)。跨度>
  • 在这些情况下,页面仍在加载,文档树尚未完成:&lt;script&gt;document.write("X");function f(){document.write("Y")}&lt;/script&gt;&lt;button onclick="f()"&gt;Write Y&lt;/button&gt;
  • @LanceRoberts:该示例与我在之前的评论中提供的代码相似,文档仍在加载中,整个文档(HTML,而不是类似图像的媒体)尚未完全解析。
  • @Lekensteyn,但我说的是相反的,第二个样本永远不会跳动,它只是在没有 close() 的情况下停止。
  • @Lekensteyn,所以这是一个时间问题,我只需要确保并关闭我的所有文档,因为它不是完全确定的。
猜你喜欢
  • 2011-04-07
  • 2019-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-16
  • 1970-01-01
  • 2013-05-06
相关资源
最近更新 更多