BenM
BenM

Reputation: 53246

jQuery AJAX Wait

I have constructed a PHP file which scrapes a web page (using cURL) to obtain some data, and outputs it to the screen in JSON format.

The target website involves some redirects which temporarily outputs data to my PHP file. Once the redirects have completed successfully, the JSON is presented as expected. The problem that I am encountering is that when I try to access the JSON using jQuery's $.ajax() method, it sometimes returns the incorrect data, because it isn't waiting for the redirects to complete.

My question is if it's possible to tell the AJAX request to wait a certain number of seconds before returning the data, thus allowing time for the redirects in the PHP script to execute successfully?

Please note that there is no cleaner solution for the page scrape, the redirects are essential and have to be outputted to the screen for the scraping to complete.

Upvotes: 0

Views: 553

Answers (4)

Pankaj Doshi
Pankaj Doshi

Reputation: 1

http://examples.hmp.is.it/ajaxProgressUpdater/

$i=0;
while (true)
{
if (self::$driver->executeScript("return $.active == 0")) {
break;
}

if($i == 20) {
break;
}

$i++;`enter code here`
echo $i;
usleep(10000);
}

Upvotes: 0

Piotr Rochala
Piotr Rochala

Reputation: 7781

You could always store the result of your ajax call and then wait for the redirects to finsih, i.e.:

$.ajax({
    success: function(e)
    {
         var wait = setTimeout(function(){ doSomethingWithData(e.data); }, 5000); //5 sec
    }

})

Alternatively, you could set up an Interval to check if something happened (redirect finished) every x amount of ms. I'm assuming your redirects are letting you know they completed?

Upvotes: 0

Bernd Haug
Bernd Haug

Reputation: 2169

There's always timeout in the settings.

jQuery docs:

timeout Number

Set a timeout (in milliseconds) for the request. This will override any global timeout set with $.ajaxSetup(). The timeout period starts at the point the $.ajax call is made; if several other requests are in progress and the browser has no connections available, it is possible for a request to time out before it can be sent. In jQuery 1.4.x and below, the XMLHttpRequest object will be in an invalid state if the request times out; accessing any object members may throw an exception. In Firefox 3.0+ only, script and JSONP requests cannot be cancelled by a timeout; the script will run even if it arrives after the timeout period.

Upvotes: 1

Ken Wheeler
Ken Wheeler

Reputation: 1958

You should use promise() in jQuery.

Upvotes: 0

Related Questions