how can I call php function from setInterval() in javascript? -
i want call php function after every 10 second, using javascript setinterval. can assist in syntax.
it never idea use setinterval call server process. server may not have finished work when gets next call , before returns something.
since have ajax, can use callback re-issue call.
for example in jquery - technique same in xmlhttprequest's readystate change
function callserver() { $.get("myserverprocess.php",function(data) { $("#somecontainer").html(data); settimeout(callserver,10000); }); }
note: above not try again if fails. if need 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 keep calling document.getelementbyid("someontainer").innerhtml=x.responsetext settimeout(callserver,10000); } x.send(); }
Comments
Post a Comment