Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/410.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么body onload=javascript函数会连续执行?_Javascript_Ajax_Onload - Fatal编程技术网

为什么body onload=javascript函数会连续执行?

为什么body onload=javascript函数会连续执行?,javascript,ajax,onload,Javascript,Ajax,Onload,很抱歉问了一个新手问题,刚刚开始学习AJAX 我想了解,当“myName”文本发生变化时,究竟是什么原因导致divMessage内容不断变化 1) Javascript函数进程似乎一直在“监听”,这是正常的脚本行为,还是存在重复调用“进程”的触发器? 2) 我们分配给“body onload”的任何函数都会重复执行,这是真的吗?这种重复执行的频率是多少? 3) 如果我们想要函数过程的一次执行,怎么做 4) 我很困惑,因为我认为身体只会加载一次。但这是不是因为在“body onload”之后调用了

很抱歉问了一个新手问题,刚刚开始学习AJAX

我想了解,当“myName”文本发生变化时,究竟是什么原因导致divMessage内容不断变化

1) Javascript函数进程似乎一直在“监听”,这是正常的脚本行为,还是存在重复调用“进程”的触发器? 2) 我们分配给“body onload”的任何函数都会重复执行,这是真的吗?这种重复执行的频率是多少? 3) 如果我们想要函数过程的一次执行,怎么做

4) 我很困惑,因为我认为身体只会加载一次。但这是不是因为在“body onload”之后调用了函数“process”,而函数进程又通过更改divMessage来修改“body”,本质上让它再次通过“body onload”,然后再次通过“process”等等

非常感谢您的帮助

<head>
  <script type="text/javascript" src="quickstart.js"></script>
</head>
<body onload='process()'>
  Server wants to know your name:
  <input type="text" id="myName" />
  <div id="divMessage" />
</body>

服务器想知道您的姓名:
下面是quickstart.js处理部件

function process()
{
  // proceed only if the xmlHttp object isn't busy
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
  {
    // retrieve the name typed by the user on the form
    name = encodeURIComponent(
    document.getElementById("myName").value);
    // execute the quickstart.php page from the server
    xmlHttp.open("GET", "quickstart.php?name=" + name, true);
    // define the method to handle server responses
    xmlHttp.onreadystatechange = handleServerResponse;
    // make the server request
    xmlHttp.send(null);
  }
}

  // callback function executed when a message is received from the server
  function handleServerResponse()
  {
    // move forward only if the transaction has completed
    if (xmlHttp.readyState == 4)
    {
      // status of 200 indicates the transaction completed successfully
      if (xmlHttp.status == 200)
      {
        xmlResponse = xmlHttp.responseXML;
        xmlDocumentElement = xmlResponse.documentElement;
        helloMessage = xmlDocumentElement.firstChild.data;

        // display the data received from the server
        document.getElementById("divMessage").innerHTML =
                  '<i>' + helloMessage+ '</i>';
      }
    }
  }
函数过程()
{
//仅当xmlHttp对象不忙时才继续
if(xmlHttp.readyState==4 | | xmlHttp.readyState==0)
{
//检索用户在表单上键入的名称
name=encodeURIComponent(
document.getElementById(“myName”).value);
//从服务器执行quickstart.php页面
open(“GET”,“quickstart.php?name=“+name,true”);
//定义处理服务器响应的方法
xmlHttp.onreadystatechange=handleServerResponse;
//发出服务器请求
xmlHttp.send(空);
}
}
//从服务器接收消息时执行的回调函数
函数handleServerResponse()
{
//仅当事务已完成时才向前移动
if(xmlHttp.readyState==4)
{
//状态为200表示事务已成功完成
if(xmlHttp.status==200)
{
xmlResponse=xmlHttp.responseXML;
xmlDocumentElement=xmlResponse.documentElement;
helloMessage=xmlDocumentElement.firstChild.data;
//显示从服务器接收的数据
document.getElementById(“divMessage”).innerHTML=
''+helloMessage+'';
}
}
}
还有quickstart.php

<?php
header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';
echo '<response>';
$name = $_GET['name'];

// generate output depending on the user name received from client
$userNames = array('YODA', 'AUDRA', 'BOGDAN', 'CRISTIAN');
if (in_array(strtoupper($name), $userNames))
  echo 'Hello, master ' . htmlentities($name) . '!';
else if (trim($name) == '')
  echo 'Stranger, please tell me your name!';
else
  echo htmlentities($name) . ', I don't know you!';

echo '</response>';
?>

通过关闭并尝试再次运行此操作找到了答案。
原来还有我注释掉的附加代码
但缓存了每1秒从handleServerResponse()调用一次“进程”:

//显示从服务器接收的数据
document.getElementById(“divMessage”).innerHTML=
''+helloMessage+'';
setTimeout('process()',1000);
很抱歉。第一课是在测试每个代码更改之前清除浏览器缓存:) 所以我想检查这个函数是否从其他地方调用的建议是正确的。 无论如何,我读了很多书,学到了很多东西
这件事已经解决了。非常感谢@pst

它(文档加载事件)应该只执行一次,除非出现一些浏览器怪癖(旧IE和iFrame?)。我知道重新打开文档对象(例如document.write)是否应该重新触发它,但是DOM操作(即使是
innetHTML
)通常也不应该。。或者也许我已经使用jQuery太久了,我忘记了它是多么的简单,IDK。在任何情况下,请用浏览器标记这是在中观察到的。请看这里:(也许)。或者,
process
是否从任何其他地方(除了onload)调用?@pst尝试了Firefox14.0.1和IE8。这种行为其实不是问题,只是想知道连续AJAX调用的机制。不,除了onload之外,没有其他要处理的调用。。谢谢我不认为观察到的行为是它看起来的样子。。使用
innerHTML+=
,而不是
innerHTML=
;它真的会重复追加(就像在中一样,HTML会不断增长)?
// display the data received from the server
    document.getElementById("divMessage").innerHTML =
              '<i>' + helloMessage+ '</i>';
    setTimeout('process()', 1000);