(function (d, w, undefined) {
var ww = new Worker(getInlineJS());
var tresholdselector = d.querySelector('#treshold');
var print = Helpers.log2Screen;
var chunkMssg = d.querySelector("#chunking");
var chunks = function () {
var div = d.createElement('div');
d.body.appendChild(div);
return div;
}();
var chunk = function () {
var span = d.createElement('span');
span.className = "numbers";
return span;
}();
var nchunks;
tresholdselector.addEventListener('change', start);
start();
ww.onmessage = function (e) {
// status message during calculation
if (e.data.status) {
return print( 'still working ... found <code>' + e.data.found +
'</code> prime numbers',
{clear: true, direct: true} );
}
// chunked writing for large result set
if (e.data.chunk) {
var nwChunk = chunk.cloneNode();
nwChunk.innerHTML = '<br>' + e.data.chunk;
chunks.appendChild( nwChunk );
chunkMssg.textContent = e.data.text;
}
$('#warning').addClass('hidden');
// print result
print( e.data.msg, ', calculation took ', e.data.duration,
' millisecond', e.data.duration != 1 ? 's': '',
'<br><b>Prime numbers found</b>:<br>', {clear:true} );
// if n of primes > 10000 write them in chunks of 1000 to screen,
// using the webworker. Else just write to screen.
if( e.data.primes && e.data.primes.length > 10000 ) {
nchunks = Math.floor(e.data.primes.length/250) + 1;
ww.postMessage({writechunked: e.data.primes, nchunks: nchunks});
} else {
print('<br><div class="numbers">',e.data.primes.join(' '), '</div>');
}
};
function start() {
chunks.innerHTML = "";
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.