Make AJAX call wait for event in php

2019-04-10 20:20发布

问题:

I do not know if my tile line is clear enough... My problem is: I have a JS application that needs to wait for an event on the server. At the moment it polls continuous the server data via XMLHttpRequest every second.

What I am thinking about is: Is it possible to make the call wait until for example a variable in PHP changes?

I hope that my question is clear enough.

Thanks!

回答1:

You're looking for long polling (colloquially known as Comet). There are many examples on SO and elsewhere.

  • http://blog.perplexedlabs.com/2009/05/04/php-jquery-ajax-javascript-long-polling/
  • How do I implement basic "Long Polling"?
  • http://www.contentwithstyle.co.uk/content/long-polling-example-with-nodejs
  • http://www.zeitoun.net/articles/comet_and_php/start