split the prime computation into smaller chunck to keep the UI thread responsive
authorAnssi Kostiainen <anssi.kostiainen@nokia.com>
Thu, 22 Nov 2012 15:26:01 +0200
changeset 280 f4caa3f58581
parent 279 513bcbcd497d
child 281 12484e4ad7c6
split the prime computation into smaller chunck to keep the UI thread responsive
battery/tests/submissions/anssik/battery-discharging.html
battery/tests/submissions/anssik/battery-unplugged.html
battery/tests/submissions/anssik/prime.js
--- a/battery/tests/submissions/anssik/battery-discharging.html	Thu Nov 22 12:44:32 2012 +0200
+++ b/battery/tests/submissions/anssik/battery-discharging.html	Thu Nov 22 15:26:01 2012 +0200
@@ -61,6 +61,9 @@
       navigator.battery.onlevelchange = onlevelchange_test.step_func(function (e) {
         assert_true(navigator.battery.level < battery_level, 'The value of the level attribute must decrease.');
         onlevelchange_test.done();
+      });
+      
+      add_completion_callback(function () {
         w.terminate();
       });
       
--- a/battery/tests/submissions/anssik/battery-unplugged.html	Thu Nov 22 12:44:32 2012 +0200
+++ b/battery/tests/submissions/anssik/battery-unplugged.html	Thu Nov 22 15:26:01 2012 +0200
@@ -21,6 +21,9 @@
         The battery must not be full or reach full capacity during the time the test is run.
       </li>
     </ol>
+    <p>
+      The highest prime number discovered so far is:  <output id="prime"></output>
+    </p>
     <div id="log"></div>
     <script>
     (function() {
@@ -64,10 +67,13 @@
       navigator.battery.onlevelchange = onlevelchange_test.step_func(function (e) {
         assert_true(navigator.battery.level > 0 && navigator.battery.level < 1.0, 'The level attribute must be set to the current battery level scaled from 0 to 1.0.');
         onlevelchange_test.done();
+      });
+
+      add_completion_callback(function () {
         w.terminate();
       });
 
-      alert('Unplug in the charger, and click OK.');
+      alert('Unplug the charger, and click OK.');
 
       })();
     </script>
--- a/battery/tests/submissions/anssik/prime.js	Thu Nov 22 12:44:32 2012 +0200
+++ b/battery/tests/submissions/anssik/prime.js	Thu Nov 22 15:26:01 2012 +0200
@@ -1,4 +1,4 @@
-// http://html5demos.com/worker
+// adapted from http://html5demos.com/worker
 
 var running = false;
 
@@ -6,20 +6,30 @@
   // doesn't matter what the message is, just toggle the worker
   if (running == false) {
     running = true;
-    run();
+    run(1);
   } else {
     running = false;
   }
 };
 
-function run() {
-  var n = 1;
-  search: while (running) {
+function run(n) {
+  // split the task into 20k chunks
+  var limit = n + 20000;
+  search: while (running && n < limit) {
     n += 1;
-    for (var i = 2; i <= Math.sqrt(n); i += 1)
-      if (n % i == 0)
-       continue search;
+    for (var i = 2; i <= Math.sqrt(n); i += 1) {
+      if (n % i == 0) {
+        continue search;
+      }
+    }
     // found a prime!
     postMessage(n);
   }
+  if (n === limit) {
+    // wait for the UI thread to update itself
+    setTimeout(function(start_time) {
+      // resume prime computation at n
+      run(n);
+    }, 150);
+  }
 }
\ No newline at end of file