I want to call a php function after every 10 second, for that I am using javascript setInterval. can anyone assist in syntax.
Asked
Active
Viewed 5,481 times
1
-
use ajax to achieve that... – Luthando Ntsekwa Jun 04 '15 at 05:48
-
Use setInterval(AjaxcalltoMethod,10000) – AkshayJ Jun 04 '15 at 05:50
2 Answers
4
It is never a good idea to use setInterval to call a server process. The server may not have finished the work when it gets the next call and before it returns something.
Since we have Ajax, we can use the callback to re-issue the call.
For example in jQuery - the technique would be the same in the XMLHttpRequest's readystate change
function callServer() {
$.get("myserverprocess.php",function(data) {
$("#somecontainer").html(data);
setTimeout(callServer,10000);
});
}
NOTE: The above will not try again if it fails. If you need it to try again, use this
function callServer() {
$.ajax({
url: "myserverprocess.php",
type: 'get'
})
.done(function(data) {
$("#somecontainer").html(data);
})
.always(function() {
setTimeout(callServer,10000);
})
.fail(function() {
$("#somecontainer").html("Error");
});
}
In plain JS:
function callServer() {
var x = new XMLHttpRequest();
x.open("GET", "myserverprocess.php", true);
x.onreadystatechange = function() {
if (x.readyState==4 && x.status==200) { // remove status test to keep calling
document.getElementById("someontainer").innerHTML=x.responseText
setTimeout(callServer,10000);
}
x.send();
}

mplungjan
- 169,008
- 28
- 173
- 236
-
The OP made no mention of jQuery in his question but this is a valid answer. Can you explain why this doesn't interrupt the server, please? It seems like they will have the same behavior. – nico Jun 04 '15 at 06:04
-
1It will not call again until after successful return from the server. I use jQuery for brevity but the setTimeout can be used without being changed inside the successful readystatechange event in plain JS – mplungjan Jun 04 '15 at 06:10
-
1
-
1The callback function for `$.get()` is only called upon success, so if there's any server error it will not make another request; this can be overcome by using `$.ajax()` instead and setting the new timeout in the complete handler. – Ja͢ck Jun 04 '15 at 06:15
-
what I basically want is, I have one file which getting update in every interval, i want to show that updated file using php after every 10 sec and overwrite what is present earlier. and to call php function I used javascript. can anyone assist in CODING? – anish Jun 04 '15 at 06:55
-
0
Say you have script.php where you run your PHP function. Client side:
var working = false;
setInterval(ajaxCall, 10000); //10000 MS == 10 seconds
function ajaxCall() {
if(working)
return;
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 == XMLHttpRequest.DONE ) {
working = false;
if(xmlhttp.status == 200){
console.log(xmlhttp.responseText);
}
}
}
xmlhttp.open("POST", "script.php", true);
xmlhttp.send();
working = true;
}
See a duplicate question (where I took the above code): how do you set interval to ajax call in jquery [closed] and jQuery-less AJAX call.
-
-
I do not recommend this. If the server is not done before we call it, we will interrupt the server process – mplungjan Jun 04 '15 at 06:00
-
@mplungjan Raises a good point, so I'd additionally just set a variable to while you're waiting for a response and set it off when you receive a reply. If the variable is off then don't do anything. – nico Jun 04 '15 at 06:07
-
Why would I bother. Just simply call the next one inside the success. – mplungjan Jun 04 '15 at 06:10
-
what I basically want is, I have one file which getting update in every interval, i want to show that updated file using php after every 10 sec and overwrite what is present earlier. and to call php function I used javascript. can anyone assist in CODING? – anish Jun 04 '15 at 06:47