0

I have a javascript function that calls a php to update DB data. After the data is updated in the DB I want to refresh the page, but I have to wait until the PHP functions finish running.

As you can see, I'm using setTimeout to reload the page after 6 seconds.

I wanted to know how to detect if the php finshed running without specifying a time window?

This is the JavaScript code:

function refresh() {
    http.abort();
    http.open("GET", "searchfiles.php");
    http.send(null);
    var timeoutID = window.setTimeout("location.reload(true);", 6000);  
}
rrk
  • 15,677
  • 4
  • 29
  • 45
Ore T
  • 29
  • 5

1 Answers1

0

I think (?) this may be what you were after - it refreshes the page once the ajax request has done it's task.

function refresh() {
    http.abort();

    /* 
       the default is to do a synchronous request which blocks, 
       alternatively set the third argument to `open` as true 
       to make it asynchronous
       eg: http.open('GET',url, true );
    */
    http.open("GET", "searchfiles.php");
    http.onreadystatechange=function(){
        if( http.readyState==4 && http.status==200 ){
            location.reload( true );
        }
    };
    http.send(null);
}
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46