简体   繁体   English

每秒加载内容,无需单击按钮

[英]Loading content every second without button click

Im trying to create a page where the requested page refreshes after a set interval. 我正在尝试创建一个页面,该页面在设置的时间间隔后刷新请求的页面。

I grabbed some basic code of w3schools which uses a button. 我获取了使用按钮的w3schools的一些基本代码。 However, i want the script to load and display the content without the use of pressing a button and refreshes after a set interval. 但是,我希望脚本在不使用按键的情况下加载和显示内容,并在设置的间隔后刷新。

I do not want to use Jquery but AJAX instead. 我不想使用Jquery,而是使用AJAX。

This loads the content onto the page in the div after pressing the button, but would like no button click and refreshes say after 5 seconds. 按下按钮后,这会将内容加载到div中的页面上,但不希望单击按钮,而是在5秒钟后刷新。

<script>
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
}
xmlhttp.open("GET","content.php",true);
xmlhttp.send();
}

</script>
</head>
<body>

<h2>AJAX</h2>
<button type="button" onclick="loadXMLDoc()">Request data</button>
setInterval(function(){loadXMLDoc();},5000);

or 要么
because you have to call only one method and no arguments you have to pass so use the second one 因为您只需要调用一个方法而不必传递任何参数,所以请使用第二个方法

setInterval(loadXMLDoc,5000);//preffered

UPDATE:- 更新: -

<script>
setInterval(loadXMLDoc,5000);//preffered
</script>
setInterval(function(){
    "content you want to refresh"
},5000);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM