Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //1: done, 2: cancelled, other: pending
  2. function getPrintingStatus(){
  3.     var d = $.Deferred();
  4.     $.post(
  5.         "/echo/json/",
  6.         {
  7.             json: JSON.stringify( {status: Math.floor(Math.random()*8+1)} ),
  8.             delay: 2
  9.         }
  10.     ).done(function(s){
  11.         d.resolve(s.status);
  12.     }).fail(d.reject);
  13.     return d.promise();
  14. }
  15.  
  16. function pollUntilDone(){
  17.     //do something
  18.     return getPrintingStatus()
  19.             .pipe(function(s){
  20.                 if(s === 1 || s == 2) {
  21.                     return s;  //if the status is done or cancelled return the status
  22.                 }
  23.                 //if the status is pending... call this same function
  24.                 //and return a deferred...
  25.                 return pollUntilDone();
  26.             });
  27. }
  28.  
  29. $.blockUI({message: "Loading..."});
  30.  
  31. pollUntilDone()
  32.     .pipe(function(s){ //project the status code to a meaningfull string.
  33.             switch(s){
  34.             case 1:
  35.                 return "done";
  36.             case 2:
  37.                 return "cancelled";
  38.             }  
  39.     })
  40.     .done(function(s){
  41.         $.unblockUI();
  42.         alert("The status is " + s);
  43.     });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement