<button id="ntpqBtn">加载NTP服务器状态</button> <pre id = "ntpq"></pre> <script> $(document).ready(function(){ $('#ntpqBtn').click( function loadOutput(){ var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function(){ if(this.readyState == 4 && this.status == 200){ var ntpqTable = document.getElementById("ntpq"); ntpq.innerHTML = this.responseText; } }; xhttp.open("GET","ntp.php",true); xhttp.send(); }); setInterval(loadOutput,1000); }); </script> 修改代码
时间: 2024-02-10 14:33:39 浏览: 31
<button id="ntpqBtn">加载NTP服务器状态</button>
<pre id="ntpq"></pre>
<script>
$(document).ready(function() {
function loadOutput() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var ntpqTable = document.getElementById("ntpq");
ntpqTable.innerHTML = this.responseText;
}
};
xhttp.open("GET", "ntp.php", true);
xhttp.send();
}
$('#ntpqBtn').click(loadOutput);
setInterval(loadOutput, 1000);
});
</script>
这段代码使用了 jQuery 库来简化代码。在 `$(document).ready` 函数中定义了 `loadOutput` 函数,并将其绑定到按钮的点击事件上。同时,也保留了定时器的逻辑,每隔 1 秒钟就会执行一次 `loadOutput` 函数。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)