Advertisement
Drakim

Lesson 3

Jan 31st, 2016
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //make something happen after two thousand miliseconds (two seconds)
  2. function hello() {
  3.     alert('Hi! Two seconds passed before you saw this');
  4. }
  5. setTimeout(hello, 2000);
  6.  
  7. //This called "async", because the order that things happned is no longer in sync (in normal order)
  8. setTimeout(function(){ alert('one'); }, 500);
  9. setTimeout(function(){ alert('three'); }, 1500);
  10. setTimeout(function(){ alert('two'); }, 1000);
  11.  
  12. //Async things are pretty common in JavaScript because many tasks finish after a delay
  13.  
  14. //Let's imagine that plussing three numbers together took a really long time.
  15. function heavyMath(a, b, c) {
  16.     var result = 0;
  17.  
  18.     setTimeout(function() {
  19.         result = a + b + c;
  20.     }, 1000);
  21.  
  22.     return result;
  23. }
  24.  
  25. //Now let's use our function
  26. var answer = heavyMath(400, 350, 900);
  27. alert(answer);
  28.  
  29. //Oh no, it alerted "0" when the real answer is 1650! What happened?
  30.  
  31. //Because "return result" ran before the numbers were plussed together, due to it taking 1000 miliseconds
  32.  
  33. //Of course normally plussing together numbers wouldn't take that much time, we are only pretending in this example
  34.  
  35. //But let's keep pretending, how do we solve this problem? We solve it with a trick called "callback"
  36.  
  37. //If you recall, you can put functions in variables
  38. var myfun = function() { alert('hello'); }
  39.  
  40. //And then call the variable as it was a function
  41. myfun();
  42.  
  43. //You can also do this to argument variables in functions
  44. function doWork(myfun) {
  45.     myfun();
  46. }
  47. function hello() {
  48.     alert('hello');
  49. }
  50.  
  51. // Whatever we put as the first argument will be called as a function
  52. doWork(hello)
  53.  
  54. // We could also do it with an inline function
  55. doWork(function() { alert('this also works!'); })
  56.  
  57. //Now, let's use this knowledge to solve our earlier problem!
  58.  
  59. //Notice how I've added another argument called "callback" here
  60. function heavyMath(a, b, c, callback) {
  61.     var result = 0;
  62.  
  63.     setTimeout(function() {
  64.         result = a + b + c;
  65.         callback(result)
  66.     }, 1000);
  67.  
  68.     return result;
  69. }
  70.  
  71. //Also notice that I'm treating callback as a function, and giving it the result variable
  72.  
  73. //So now we can do this
  74.  
  75. function shout(result) {
  76.     alert('The answer was '+result)
  77. }
  78.  
  79. heavyMath(400, 350, 900, shout);
  80.  
  81. //The shout function gets put in the 4th argument named callback
  82.  
  83. //And since we do callback(result) it becomes the same as shout(result)
  84.  
  85. //And bam, it has the real result 1650 this time
  86.  
  87. //In the real world, this callback technique is used for real work that takes time
  88.  
  89. //The most common example is Ajax
  90.  
  91. //Ajax is when you tell JavaScript to go fetch a website for you and put the website in a variable
  92.  
  93. //Here I've coded up a tiny Ajax example
  94.  
  95.  
  96. var ajax = function(url, callback){
  97.     var xmlHTTP = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("MicrosoftXMLHTTP");
  98.     xmlHTTP.onreadystatechange = function(){
  99.         if(this.readyState == 4 && this.status == 200)
  100.             callback(this.responseText);
  101.     }
  102.     xmlHTTP.open("GET", url, true);
  103.     xmlHTTP.send();
  104. }
  105.  
  106. //Notice how it has an argument called callback? That's telling us that we should put a function in there
  107.  
  108. //Try visiting this website in your browser first: http://jsonplaceholder.typicode.com/posts/1
  109.  
  110. //It gives you some random data, let's fetch it with Ajax instead of using a browser
  111.  
  112. function shout(message) {
  113.   alert(message);
  114. }
  115.  
  116. ajax('http://jsonplaceholder.typicode.com/posts/1', shout);
  117.  
  118. //So this essensially means, visit this website, and give the result to the shout function
  119.  
  120. //Ajax is async so doing this wouldn't work:
  121. var answer = ajax('http://jsonplaceholder.typicode.com/posts/1');
  122.  
  123. //The answer will always be empty because it gives you the result before it's actually done the job
  124.  
  125. //That's why it's important to know if a function is sync (normal) or async.
  126.  
  127. //If it has a callback argument, you can pretty much assume it's async
  128.  
  129. //But if it says "return" at the end, it means it's giving you the answer instantly (sync)
  130.  
  131. //Async functions with callback are very popular in JavaScript, you'll see them everywhere
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement