The JavaScript code example demonstrates how to program a mutlithreaaded worker that uses a built-in blob script.
<!DOCTYPE html> <html> <head> <title>XoaX.net's Javascript</title> <script type="text/javascript" src="BlobScript.js"></script> </head> <body onload="Initialize()"> <label for="idFactorization"><b id="idN"></b> = <input id="idFactorization" type="text" size="21" /></label> </body> </html>
// NOTE: THIS REQUIRES A WEB SERVER TO RUN function Initialize() { // Create a worker with a Blob as its script var qWorker = new Worker(URL.createObjectURL(new Blob([ ` function Factor(iN) { var iaFactors = []; var iM = iN; while (iM > 1) { for (var i = 2; i <= iM; ++i) { while ((iM % i) == 0) { iaFactors.push(i); iM /= i; } } } return iaFactors; } onmessage = function(event) { const kiN = parseInt(event.data); var iaFactors = Factor(kiN); postMessage(iaFactors); };`], { type: "text/javascript" }))); // Receive factorization messages from the worker qWorker.onmessage = function(qEvent) { var iaFactors = qEvent.data; var sFactoriation = ""; for (var i = 0; i < iaFactors.length - 1; ++i) { sFactoriation += iaFactors[i] + " * "; } sFactoriation += iaFactors[iaFactors.length - 1]; var qFactElement = document.getElementById("idFactorization"); qFactElement.value = sFactoriation; } // Generate a number between 2 and 1000 var iN = Math.floor((Math.random() * 999) + 2); var qN = document.getElementById("idN"); qN.innerHTML = iN; qWorker.postMessage(iN); }
© 20072025 XoaX.net LLC. All rights reserved.