Reputation: 31
I have following codes:
JSP code is as follows:
<html>
<head>
<script src="js/nextprevpage.js" type="text/javascript"></script>
</head>
<body onload="javascript:first();">
<div id="getData">
</div>
</body>
</html>
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();
}
Now I have another jsp page as updatesession.jsp.
<html>
<head>
</head>
<body ">
Hello......
</body>
</html>
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.
Upvotes: 1
Views: 13701
Reputation: 343
function first(){
/*
write your AJAX code
*/
window.setInterval(first, 10000);
first() // function recursion logic call
}
This logic will run every interval to call AJAX send operation
Upvotes: -1
Reputation: 22721
You can use setInterval()
Syntax: window.setInterval(func, delay);
function first(){
....
xmlhttp.send();
window.setInterval(first, 10000);
}
Upvotes: 6
Reputation: 793
You can use JavaScript Timing Events http://www.w3schools.com/js/js_timing.asp to call js function after certain interval of time.
You can do like this:
setInterval(function(){alert("Hello")},3000);
Upvotes: 0
Reputation: 4525
Put your method into:
window.setInterval(yourfunction(){},time_in_millis);
Upvotes: 0
Reputation: 476
You need to use window.setInterval
for this.
Check http://www.w3schools.com/js/js_timing.asp
window.setInterval("first", 5000);
Upvotes: 1
Reputation: 3584
Use window.setInterval
function to keep calling a function repeatedly.
Upvotes: 3