Stop AJAX Request After Time Limit

I’ve got an AJAX request going to a PHP proxy which uses a 3rd party service to grab some data I need. The speed of this 3rd party system is variable, and I would like to cancel the request after 5 seconds if it hasn’t gone through. Does anyone know how to do this?

I could run a timer in JavaScript - but even when I realize 5 seconds have gone by, I don’t know how to actually cancel that AJAX request. The request will eventually come back and run the JavaScript callback function which will not be good.

I tried using PHP’s set_time_limit, but for some reason its not working. In fact, testing with a sleep() function, I found that I have to exceed 60 seconds to hit the PHP time limit.

I also thought about just running in a tight loop and checking the time. However, to make this work I would need to be running some kind of multi-threaded system. I don’t even know if PHP supports threading.

So any help with this problem would be appreciated.

Thanks.