如何才能不让'沙漏'JavaScript时完成(停止活动指示器)的浏览器? [英] How do you stop browser from 'hourglassing' when Javascript is DONE (stop throbber)?

查看:175
本文介绍了如何才能不让'沙漏'JavaScript时完成(停止活动指示器)的浏览器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

写一个小的HTML网页,一些非常简单的JavaScript中,我注意到有人做过之后,它保持了一圈纺(火狐)。我已经看到了在其他页面上,总是很多次都认为是一个JavaScript错误或循环的问题,但这个方案我跑肯定是完成的。

下面是一个小的网页,这是否的一个例子。当您单击按钮,它处理的活动指示器(感谢Kooilinc为期限)不断持续。

 <!DOCTYPE HTML PUBLIC -  // W3C // DTD XHTML 1.0过渡// EN
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
< HTML的xmlns =htt​​p://www.w3.org/1999/xhtml>
  < HEAD>
    < META HTTP-EQUIV =内容类型内容=text / html的;字符集= UTF-8/>
    <标题>年级仪< /标题>
    < SCRIPT LANGUAGE =JavaScript的类型=文/ JavaScript的>
      函数计算()
      {
       VAR级=的document.getElementById(等级)value.toUpperCase()。
        开关(级)
        {
         案A:
           的document.write(杰出成就);
           打破;         案B:
           文件撰写(高于平均水平);
           打破;         案C:
           的document.write(平均成绩);
           打破;          案D:
            文件撰写(低及格分数);
           打破;         字母f:
           文件撰写(不及格);
            打破;
        }
      }
    < / SCRIPT>
    < /头>
  <身体GT;
    等级:LT;输入类型=文本ID =级/>
    < BR /><输入类型=按钮值=获取结果的onclick =计算()/>
  < /身体GT;
< / HTML>

我可以通过按下stop停止它(右键点击并停止)。

我有什么做的就是JavaScript来停止处理(或浏览器停止处理)脚本完成后?

注1:这发生在FF4.01,而不是在IE或Chrome(此code样品)

注2:我试着开关语句后window.stop()函数,它并没有停止活动指示器

根据Lekensteyn的回答,document.close()switch语句后解决了这一个,但这里有一个根本没有感到紧张,并且不使用document.close(例子)。

 <!DOCTYPE HTML PUBLIC -  // W3C // DTD XHTML 1.0过渡// EN
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
< HTML的xmlns =htt​​p://www.w3.org/1999/xhtml>
  < HEAD>
    < META HTTP-EQUIV =内容类型内容=text / html的;字符集= UTF-8/>
    <标题>实验室9.1< /标题>
  < /头>
  <身体GT;
    < SCRIPT LANGUAGE =JavaScript的类型=文/ JavaScript的>
      VAR短语=每一个好孩子不罚款的推移就行了。
      的document.write(phrase.length +< BR />中);
      对于(VAR I = 0; I< phrase.length;我++)
      {
        的document.write(phrase.char $ C $猫(我)+);
      }
        文件撰写(< BR />中);
        的document.write(phrase.toLowerCase()+< BR />中);
        的document.write(phrase.toUpperCase()+< BR />中);
    < / SCRIPT>
  < /身体GT;
< / HTML>


解决方案

在调用后的document.write(),则必须调用 document.close (),如果该文件是previously没有关闭。

基本上,发生这种情况:


  1. 页面是越来越解析

  2. 脚本进行处理

  3. 页是封闭( document.close()

如果的document.write()被称为第2步中,将在第3步完成。
除了第3步,如果你调用文件撰写,你再次打开该页面,但在FF的页面不会关闭本身(不知道其他人)。你已经通过调用来关闭它 document.close()

我发现这一点,当我需要打开一个窗口,并在FF迅速写了进去。

  VAR赢= window.open();
win.document.write(东西);
//需要下一行以完成页面
win.document.close();

这同样适用于你的情况:

 <脚本类型=文/ JavaScript的>
函数计算()
{
    VAR级=的document.getElementById(等级)value.toUpperCase()。
    开关(级)
    {
    案A:
        的document.write(杰出成就);
        打破;
    //去掉某些情况下,
    字母f:
        文件撰写(不及格);
        打破;
    }
    //完成文档
    document.close();
}
< / SCRIPT>

Writing a small HTML web page with some very simple Javascript in it, I noticed that after it was done, it kept the circle spinning (firefox). I've seen that many times on other pages and always thought that it was a Javascript error or loop problem, but this program I was running was definitely done.

Here is one example of a small web page that does this. After you click the button and it processes, the throbber (thanks for Kooilinc for the term) keeps on going.

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

I was able to stop it by hitting stop (right-click and stop).

What do I have to do to get Javascript to stop processing (or the browser to stop processing) after the script is done?

Note1: This occurred in FF4.01, not in IE or Chrome (for this code sample).

Note2: I tried the window.stop() function after the switch statement, and it didn't stop the throbber.

As per Lekensteyn's answer, document.close() after the switch statement solved this one, but here's an example that didn't throb at all and doesn't use 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>

解决方案

After calling document.write(), you must call document.close() if the document was previously not closed.

Basically, this happens:

  1. The page is getting parsed
  2. Scripts are processed
  3. Page is "closed" (document.close())

If document.write() is called within step 2, it will be finished in step 3. Beyond step 3, if you call document.write, you're opening the page again, but the page won't close itself in FF (not sure about others). You've to close it by calling document.close().

I discovered this when I needed to open a window and write into it quickly in FF.

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

The same applies to your case:

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

这篇关于如何才能不让'沙漏'JavaScript时完成(停止活动指示器)的浏览器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆