如何每5秒连续调用JS函数 [英] How to call JS function continuously after every 5 seconds
本文介绍了如何每5秒连续调用JS函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码:
JSP代码如下:
<html>
<head>
<script src="js/nextprevpage.js" type="text/javascript"></script>
</head>
<body onload="javascript:first();">
<div id="getData">
</div>
</body>
</html>
nextprevpage.js中存在的代码如下:
Code present in nextprevpage.js is as follows:
function first()
{
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("getData").innerHTML= xmlhttp.responseText;
}
}
// here you make a request to your script to check your database...
xmlhttp.open("POST", "updatesession.jsp?", true);
xmlhttp.send();
}
现在,我还有另一个jsp页面作为updatesession.jsp.
Now I have another jsp page as updatesession.jsp.
<html>
<head>
</head>
<body ">
Hello......
</body>
</html>
我使用onload()事件在上述js函数中进行了调用.我需要连续调用此js函数.5秒后是否有任何方法可以调用此函数.
I called above js function using onload() event. I need to call this js function continuously. Is there any way to call this function every after 5 seconds.
推荐答案
您可以使用语法: window.setInterval(func,delay);
function first(){
....
xmlhttp.send();
window.setInterval(first, 10000);
}
这篇关于如何每5秒连续调用JS函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文