Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. //timed update
  2. function refresh() {
  3.  
  4. if (BLOCKED != 1) {
  5. BLOCKED = 1;
  6. $.ajax({
  7. url: 'update',
  8. success: function(data) {
  9. BLOCKED = 0;
  10. }
  11. });
  12. }
  13.  
  14. //click function
  15. function load() {
  16. if (BLOCKED != 1) {
  17. BLOCKED = 1;
  18. $.ajax({
  19. url: '/load'
  20. });
  21. } else setTimeout("load()", 500);
  22. }
  23.  
  24. // global state
  25. var inFlight = false;
  26. var pendingLoad = false;
  27.  
  28. function checkPendingLoad() {
  29. if (pendingLoad) {
  30. pendingLoad = false;
  31. load();
  32. }
  33. }
  34.  
  35. function refresh() {
  36. if (!inFlight) {
  37. inflight = true;
  38. $.ajax({
  39. url: 'update',
  40. complete: function(data) {
  41. inFlight = false;
  42. checkPendingLoad();
  43. }
  44. });
  45. }
  46. }
  47.  
  48.  
  49. //click function
  50. function load() {
  51. if (!inFlight) {
  52. inFlight = true;
  53. $.ajax({
  54. url: '/load',
  55. complete: function(data) {
  56. inFlight = false;
  57. checkPendingLoad();
  58. }
  59. });
  60. } else {
  61. pendingLoad = true;
  62. }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement