Skip to content

Instantly share code, notes, and snippets.

@mrfabbri
Created September 19, 2010 19:34
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 mrfabbri/587050 to your computer and use it in GitHub Desktop.
Save mrfabbri/587050 to your computer and use it in GitHub Desktop.
A quick scaffolding html file to load a web worker at runtime, a sample web worker file is provided. Aimed at tinkering with project euler problems in JavaScript without freezing the browser.
<html>
<head>
<title>
</title>
<style>
#input-area{
width: 400px;
margin: 10px;
}
#output-area{
width: 400px;
height: 100px;
margin: 10px;
}
</style>
<script>
var evalWorker;
function initEvalWorker(workerJSFile)
{
evalWorker = new Worker(workerJSFile);
evalWorker.addEventListener('message', function(event)
{
document.getElementById("output-area").textContent = event.data;
}, false);
}
function stop()
{
evalWorker.terminate();
}
function rep()
{
initEvalWorker(document.getElementById("input-area").value);
evalWorker.postMessage("START");
}
</script>
</head>
<body>
<div>
worker JavaScript file:
<input type="text" id="input-area"></textarea>
</div>
<div>
<button type="button" onclick="rep();">SOLVE</button>
<button type="button" onclick="stop();">STOP</button>
</div>
<div id="output-area">
</div>
</body>
</html>
onmessage = function (event)
{
var result;
result = 42;
postMessage(result);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment