I need to add polling mechanism to call a web service through my web page. For that I am trying to use an ajax
call inside a javascript
page. I am very new to ajax
and javascript
. I wrote the below code.
<html>
<head>
<script language="JavaScript" type="text/javascript">
function pollServerForNewMail() {
setTimeout(function(){
$.ajax({ url: "server", success: function(data){
alert("TEST");
poll();
}, dataType: "json"});
}, 10000);
</script>
</head>
<body>
</body>
</html>
What I need is to fire the alert TEST in every 10 seconds. Anybody please help me on this.
I will edit the post with my two jsp files.
index.jsp file
<html>
<table style="width:100%;text-align:center;'">
<tr>
<td style="text-align:center;width:100%">
<a href="polling.jsp?reset=true"><img src="images/import.png" /></a>
</td>
</tr>
</table>
</html>
polling.jsp file
<html>
<head>
<script language="JavaScript" type="text/javascript">
function pollServerForNewMail() {
setTimeout(function(){
$.ajax({ url: "server", success: function(data){
alert("TEST");
poll();
}, dataType: "json"});
}, 10000);
}
pollServerForNewMail() ;
</script>
</head>
<body>
</body>
</html>
Thanks
I solved my problem by changing polling.jsp file as below. I will add my solution.
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
var set_delay = 5000,
callout = function () {
$.ajax({
/* blah */
})
.done(function (response) {
alert("TEST");
})
.always(function () {
setTimeout(callout, set_delay);
});
};
callout();
</script>
</head>
<body>
</body>
</html>