Skip to content

Instantly share code, notes, and snippets.

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 AndrewBarfield/1a9e3740e47443e34b08 to your computer and use it in GitHub Desktop.
Save AndrewBarfield/1a9e3740e47443e34b08 to your computer and use it in GitHub Desktop.
Calculate Pi to 5,000 digits in a Web Worker
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<h2><img alt="" src="https://mdn.mozillademos.org/files/3831/HTML5_Performance_512.png" style="vertical-align: middle; width: 64px; height: 64px; padding-right: 0.5em;">Calculating Pi to 5,000 digits in a Web Worker</h2>
<div id="Pi_Output"></div>
<!-- To see the Web Worker script, look here: -->
<!-- http://codepen.io/AndrewBarfield/pen/NPRJwW -->
<script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
</body>
</html>
$(function() {
if (window.Worker) {
var WEB_WORKER_URL = "http://s.codepen.io/AndrewBarfield/pen/NPRJwW.js";
var worker = new Worker(WEB_WORKER_URL);
worker.onmessage = function(e) {
$("#Pi_Output").text(e.data);
}
// Start the worker
worker.postMessage("");
}
else {
$("#Pi_Output").html("<h1>Your browser does not support Web Workers.<br/>Please use a modern browser.</h1>");
}
});
/* latin */
@font-face {
font-family: 'Roboto';
font-style: normal;
font-weight: 400;
src: local('Roboto Regular'), local('Roboto-Regular'), url(http://fonts.gstatic.com/s/roboto/v14/fg2nPs59wPnJ0blURyMU3PesZW2xOQ-xsNqO47m55DA.woff2) format('woff2');
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2212, U+2215, U+E0FF, U+EFFD, U+F000;
}
body {
margin: 20px;
font-family: 'Roboto', sans-serif;
}
#Pi_Output {
word-wrap: break-word;
font-size: normal;
line-height: 150%;
letter-spacing: 1px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment