Skip to content

Instantly share code, notes, and snippets.

@crguezl
Created October 15, 2015 08:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crguezl/43107f45bc0c1ec7626c to your computer and use it in GitHub Desktop.
Save crguezl/43107f45bc0c1ec7626c to your computer and use it in GitHub Desktop.
Example of race condition taken from Concurrency and Parallel Computing in JavaScript by Stephan Herhut on Mar 05, 2014
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Asynchronous Image Loading</title>
</head>
<body>
<div id="holder-div"></div>
<script type="text/javascript">
var image = new Image(100),
url = "http://www.infoq.com/resource/presentations/javascript-concurrency-parallelism/en/promoimage/strangeloop2013.png", //myimg.jpg
container = document.getElementById("holder-div");
image.src = url;
setTimeout(function(){
image.onload = function() {
container.appendChild(image)
};
}, 1000);
</script>
<a href="http://www.infoq.com/presentations/javascript-concurrency-parallelism">Concurrency and Parallel Computing in JavaScript (Recorded at: StrangeLoop) by Stephan Herhut on Mar 05, 2014 </a>
</body>
</html>
@crguezl
Copy link
Author

crguezl commented Oct 15, 2015

See slides 10 and 11 of the talk
Concurrency and Parallel Computing in JavaScript by Stephan Herhut on Mar 05, 2014.

Play with the number of milliseconds in the setTimeout call.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment