Change page in the middle of Ajax request

前端 未结 5 564
面向向阳花
面向向阳花 2021-01-17 12:07

What happens if I send some ajax request, and immediately change the page (say follow some link) before the request returns? I mean I suppose the XHR object sends a request

5条回答
  •  时光说笑
    2021-01-17 12:22

    The solution to this problem is simple, all you need to do is if the request is currently running and if the user clicks any other link, then you need to alert the user that the request is under process please wait

    To achieve this you need to maintain a semaphore, set that semaphore before firing any request and reset it on completion of that request. On click of every link(which changes the page) call a method which checks the semaphore and if it is set then the method gives the above alert to the user. This way, until the request completes, the user wont be able to change the page, and your problem of request being aborted would be solved

    Hope this answers the question.

提交回复
热议问题