DOMWorkerThreads current: Difference between revisions

Jump to navigation Jump to search
Line 21: Line 21:
     startdiv.innerHTML = "Started: " + new Date();
     startdiv.innerHTML = "Started: " + new Date();


     function scriptToRun(){
     function scriptToRun() {
       // Hook us up to receive messages from the pool
       // Hook us up to receive messages from the pool
       this.messageListener = function(message, source) {
       this.messageListener = function (message, source) {
         var num = parseInt(message);
         var num = parseInt(message);
         if (isNaN(num)) {
         if (isNaN(num))
           throw "Pool sent a value we couldn't interpret!";
           throw "Pool sent a value we couldn't interpret!";
        }
         postMessageToPool(fibonacci(num));
         postMessageToPool(fibonacci(num));
       };
       };
Line 33: Line 32:
       // Worst implementation of fibonacci *ever*!
       // Worst implementation of fibonacci *ever*!
       function fibonacci(n) {
       function fibonacci(n) {
         if(n == 0)
         if (n == 0)
           return 0;
           return 0;
         if(n == 1)
         if (n == 1)
           return 1;
           return 1;
         return fibonacci(n - 1) + fibonacci(n - 2);
         return fibonacci(n - 1) + fibonacci(n - 2);
Line 43: Line 42:
     var wp = navigator.newWorkerPool();
     var wp = navigator.newWorkerPool();


     wp.messageListener = function(message, source) {
     wp.messageListener = function (message, source) {
       resultdiv.innerHTML = "Result from worker: " + message;
       resultdiv.innerHTML = "Result from worker: " + message;
       enddiv.innerHTML = "Finished: " + new Date();
       enddiv.innerHTML = "Finished: " + new Date();
     };
     };


     wp.errorListener = function(error, source) {
     wp.errorListener = function (error, source) {
       alert("Worker had an error: " + error);
       alert("Worker had an error: " + error);
     }
     };


     var thread = wp.createWorker(scriptToRun.toString() + "scriptToRun();");
     var thread = wp.createWorker(scriptToRun.toString() + "scriptToRun();");
149

edits

Navigation menu