Guest User

Untitled

a guest
May 23rd, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. //Since maybe you can start many AJAX requests, I'd argue that one of best solutions - basically, a reliable solution - is first creating an array of requests' state like this:
  2.  
  3. var ajaxRequestsState = [];
  4. //When you start an AJAX request, you should add an element to this array like this one:
  5.  
  6. ajaxRequestsState.push({ requestId: "some identifier" });
  7. //Later, if you need to check if there's no active request, you can have a flag like so:
  8.  
  9. function isAsyncRequestActive() {
  10. return ajaxRequestsState.length > 0;
  11. }
  12. //Finally, whenever a request ends or fails, you must do so:
  13.  
  14. function releaseRequest(requestId) {
  15. var requestIndex = 0;
  16. var found = false;
  17.  
  18. while(!found && requestIndex < ajaxRequestsState.length)
  19. {
  20. found = ajaxRequestsState[requestIndex].requestId == requestId;
  21. requestIndex++;
  22. }
  23.  
  24. if(found) {
  25. ajaxRequestsState.splice((requestIndex-1), 1);
  26. }
  27. }
  28.  
  29. releaseRequest("identifier of request which ended or failed");
  30. //That's simply tracking requests' state and maintaining a collection of requests' states, and you'll have it!
Add Comment
Please, Sign In to add comment