Created
February 13, 2013 01:01
-
-
Save jtwb/4780537 to your computer and use it in GitHub Desktop.
Asynchronous programming basics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Time elapsed: 0µs | |
var a = 200000000000; | |
for (var i = 1000; i; i--){ | |
a = a / 3; | |
} | |
// Time elapsed: 1µs, probably less | |
var exampleData = ''; | |
$.getJSON( | |
'http://example.com/data.json', | |
function(data){ | |
exampleData = data; | |
// Time elapsed: ~200000µs, after ajax request completes | |
// Value of exampleData: { live data } | |
// Second execution block terminates here | |
}) | |
); | |
// We have now fired an ajax request and registered code to execute on ajax complete | |
// Time elapsed: still 1µs | |
// Value of exampleData: '' | |
// Execution block terminates here |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment