Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Jan 12th, 2013  |  syntax: None  |  size: 1.26 KB  |  views: 16  |  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. var yourCallback = function() {
  2.     alert('2');
  3. }
  4.        
  5. function1(params, yourCallback);
  6.        
  7. function function1(args, callback) {
  8.     //Some code
  9.     if (callback) {
  10.         callback();
  11.     }
  12. }
  13.        
  14. function doSomething(arg1, callback){
  15.     // bla bla bla
  16.     callback();
  17. }
  18.        
  19. function one(x, y, z, callback) {
  20.    // do operations here
  21.  
  22.    callback();
  23. }
  24.        
  25. one(1, 2, 3, function(){alert('hello');});
  26.        
  27. var func = function() { alert('hello'); };
  28. one(1, 2, 3, func);
  29.        
  30. function func() {
  31.   alert('hello');
  32. }
  33.  
  34. one(1, 2, 3, func);
  35.        
  36. function myFunctionNeedsACallback(param1, callback) {
  37.     //do stuff with param1
  38.     if(typeof callback === "function"){
  39.         callback.call(yourThisParam, anyOtherParams);
  40.     }
  41. }
  42.        
  43. myFunctionNeedsACallback(param, function() { alert('2'); });
  44.  
  45. //will alert 2 after running the rest of the contents of the function
  46.        
  47. function ajaxCall1(){
  48.     return $.ajax(/*your parameters NO SUCCESS CALLBACK NEEDED*/);
  49. }
  50.  
  51. function ajaxCall2(){
  52.     return $.ajax(/*your parameters NO SUCCESS CALLBACK NEEDED*/);
  53. }
  54.  
  55. function ajaxCall3(){
  56.     return $.ajax(/*your parameters NO SUCCESS CALLBACK NEEDED*/);
  57. }
  58.  
  59. $.when(ajaxCall1(), ajaxCall2(), ajaxCall3()).then(function(results1, results2, results3) {
  60.     //do stuff with all 3 results without "nesting" things
  61. });