Home > Article > Backend Development > javascript - After the ajax request is terminated, how to make the requested php also terminate? ?
I made a web version of the search function.
The search is initiated by the front desk, the search is executed in the background, and the search results are returned.
When I encounter a folder search and there are a lot of files in the folder (more than 20,000), the search time is very long (it takes about 2 minutes to get the results). Sometimes I am impatient to wait and click stop. search.
<code> // 终止搜索 stopSearch: function(){ console.log('xhr请求集合: ' , this.reqList); for (var i = 0; i < this.reqList.length; ++i) { if (this.reqList[i].readyState !== 0 || this.reqList[i].status !== 200) { this.reqList[i].abort(); } } } </code>
After clicking to stop the search, the front desk has indeed stopped:
However, the background php script is still running (and I set the maximum time for the php script to run to 3 minutes). Each time the page is refreshed, it takes a long time (more than 1 minute). This is still a relatively honest situation (only one is initiated). If When the bird fire started, the mouse clicked wildly, so I guess I had to restart the server... Sigh (the following are the consequences of crazy clicking, only two or three clicks, the longest request time reached 5 minutes...)!
Is there a way to terminate the running of the background php script when stopping the request? ?
I made a web version of the search function.
The search is initiated by the front desk, the search is executed in the background, and the search results are returned.
When I encounter a folder search and there are a lot of files in the folder (more than 20,000), the search time is very long (it takes about 2 minutes to get the results). Sometimes I am impatient to wait and click stop. search.
<code> // 终止搜索 stopSearch: function(){ console.log('xhr请求集合: ' , this.reqList); for (var i = 0; i < this.reqList.length; ++i) { if (this.reqList[i].readyState !== 0 || this.reqList[i].status !== 200) { this.reqList[i].abort(); } } } </code>
After clicking to stop the search, the front desk is indeed stopped:
However, the background php script is still running (and I set the maximum time for the php script to run to 3 minutes). Each time the page is refreshed, it takes a long time (more than 1 minute). This is still a relatively honest situation (only one is initiated). If When the bird fire started, the mouse clicked wildly, so I guess I had to restart the server... Sigh (the following are the consequences of crazy clicking, only two or three clicks, the longest request time reached 5 minutes...)!
Is there a way to terminate the running of the background php script when stopping the request? ?
No, all you can do is optimize your search performance
The only way to start is by optimizing search performance. The HTTP protocol does not have a "request" to initiate a request termination to the background, and if the front-end server uses nginx, the PHP application server does not even know that the client has disconnected.
In this case, you can only start with the optimization background.