Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 1st, 2012  |  syntax: None  |  size: 2.58 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Return value based on user input
  2. function function1(){
  3.  
  4.     var myVariable=function2;
  5.     // nothing should happen at this point until function2 has returned a response
  6.     if(myVariable){
  7.         alert('true');
  8.     }else{
  9.         alert('false');
  10.     }
  11. }
  12.  
  13.  
  14. function function2(){
  15.  
  16.     var yesbutton=$("<input type='submit' value='yes' />");
  17.     var nobutton=$("<input type='submit' value='no' />");
  18.     $('body').append(yesbutton)
  19.     $('body').append(nobutton)
  20.     nobutton.click(function(){
  21.         return false;
  22.     });
  23.     yesbutton.click(function(){
  24.         return true;
  25.     });
  26.  
  27. }
  28.  
  29.  
  30. function1();
  31.        
  32. function function1(){
  33.  
  34.     var myVariable=function2();
  35.     // nothing should happen at this point until function2 has returned a response
  36.     myVariable.done(function(){
  37.         alert('true');
  38.     }).fail(function(){
  39.         alert('false');
  40.     });
  41. }
  42.  
  43.  
  44. function function2(){
  45.     var deferred = $.Deferred();
  46.  
  47.     var yesbutton=$("<input type='submit' value='yes' />");
  48.     var nobutton=$("<input type='submit' value='no' />");
  49.     $('body').append(yesbutton)
  50.     $('body').append(nobutton)
  51.     nobutton.click(deferred.reject);
  52.     yesbutton.click(deferred.resolve);
  53.  
  54.     return deferred.promise().always(function(){
  55.         yesbutton.add(nobutton).remove();
  56.     });
  57.  
  58. }
  59.  
  60.  
  61. function1();
  62.        
  63. function function1(){
  64.  
  65.     var yesbutton=$("<input type='submit' value='yes' />");
  66.     var nobutton=$("<input type='submit' value='no' />");
  67.     $('body').append(yesbutton)
  68.     $('body').append(nobutton)
  69.     nobutton.click(function(){
  70.         alert('false');
  71.     });
  72.     yesbutton.click(function(){
  73.         alert('true');
  74.     });
  75.  
  76. }
  77.        
  78. function function2(yesClickHandler, noClickHandler){
  79.     var deferred = $.Deferred();
  80.  
  81.     var yesbutton=$("<input type='submit' value='yes' />");
  82.     var nobutton=$("<input type='submit' value='no' />");
  83.     $('body').append(yesbutton).append(nobutton);
  84.  
  85.     nobutton.click(noClickHandler);
  86.     yesbutton.click(yesClickHandler);
  87.  
  88.     return deferred.promise();
  89.  
  90. }
  91.  
  92. function2(function(){
  93.    alert('true');
  94. }, function(){
  95.    alert('false');
  96. });
  97.        
  98. function function1(){
  99.     function2(function(myVariable){
  100.         if(myVariable){
  101.             alert('true');
  102.         }else{
  103.             alert('false');
  104.         }
  105.     };
  106. }
  107.  
  108.  
  109. function function2(callback){
  110.  
  111.     var yesbutton=$("<input type='submit' value='yes' />");
  112.     var nobutton=$("<input type='submit' value='no' />");
  113.     $('body').append(yesbutton)
  114.     $('body').append(nobutton)
  115.     nobutton.click(function(){
  116.         callback(false);
  117.     });
  118.     yesbutton.click(function(){
  119.         callback(true);
  120.     });
  121. }
  122.  
  123. function1();
  124.        
  125. var myVariable=function2();