2009/dap/system-info battery-status.html,1.30,1.31

Update of /sources/public/2009/dap/system-info
In directory hutz:/tmp/cvs-serv26457

Modified Files:
	battery-status.html 
Log Message:
refactor the intro example

Index: battery-status.html
===================================================================
RCS file: /sources/public/2009/dap/system-info/battery-status.html,v
retrieving revision 1.30
retrieving revision 1.31
diff -u -d -r1.30 -r1.31
--- battery-status.html	30 Jun 2011 09:45:26 -0000	1.30
+++ battery-status.html	1 Jul 2011 07:40:10 -0000	1.31
@@ -107,20 +107,25 @@
           <title>Email Client</title>
           <script>
             var mail = {
-              INTERVAL_DEFAULT: 10000,
-              interval_current: null,
+              INTERVAL_DEFAULT: 1000 * 10,
+              interval: null,
               timer: 0,
 
               check: function () {
                 console.log('Checking the server for new emails using an interval of ' + 
-                            (mail.interval_current/1000) + ' seconds.');
+                            (mail.interval / 1000) + ' seconds.');
+              },
+              
+              setTimer: function (interval) {
+                if (interval === mail.interval) { return; }
+                if (mail.timer !== 0) { clearTimeout(mail.timer); }
+                if (interval) { mail.timer = setInterval(function () { mail.check(); }, interval); }
+                mail.interval = interval;
               }
             };
 
             window.addEventListener('load', function () {
-              var interval = (!mail.interval_current) ? mail.INTERVAL_DEFAULT : mail.interval_current;
-              mail.timer = setInterval(function () { mail.check(); }, interval);
-              mail.interval_current = interval;
+              mail.setTimer(!mail.interval ? mail.INTERVAL_DEFAULT : mail.interval);
             }, false);
           </script>
         </head>
@@ -148,39 +153,40 @@
             var mail = {
               INTERVAL_BATTERY_LOW: 1000 * 60 * 10,
               INTERVAL_DEFAULT: 1000 * 10,
-              interval_current: null,
+              interval: null,
               timer: 0,
               
               check: function () {
                 console.log('Checking the server for new emails using an interval of ' + 
-                            (mail.interval_current/1000) + ' seconds.');
+                            (mail.interval / 1000) + ' seconds.');
+              },
+              
+              setTimer: function (interval) {
+                if (interval === mail.interval) { return; }
+                if (mail.timer !== 0) { clearTimeout(mail.timer); }
+                if (interval) { mail.timer = setInterval(function () { mail.check(); }, interval); }
+                mail.interval = interval;
               }
             };
             
             window.addEventListener('load', function () {
-              mail.interval_current = (!mail.interval_current) ? mail.INTERVAL_DEFAULT : mail.interval_current;
-              mail.timer = setInterval(function () { mail.check(); }, mail.interval_current);
+              mail.setTimer(!mail.interval ? mail.INTERVAL_DEFAULT : mail.interval);
             }, false);
             
             window.addEventListener('batterystatus', function (battery) {
-              if (battery.isPlugged && (mail.interval_current !== mail.INTERVAL_DEFAULT)) {
-                clearTimeout(mail.timer);
-                mail.interval_current = mail.INTERVAL_DEFAULT;
-                mail.timer = setInterval(function () { mail.check(); }, mail.interval_current);
+              if (battery.isPlugged && (mail.interval !== mail.INTERVAL_DEFAULT)) {
+                mail.setTimer(mail.INTERVAL_DEFAULT);
                 console.log('Plugged in, checking the server normally.');
               }
             }, false);
             
             window.addEventListener('batterylow', function () {
-              clearTimeout(mail.timer);
-              mail.interval_current = mail.INTERVAL_BATTERY_LOW;
-              mail.timer = setInterval(function () { mail.check(); }, mail.interval_current);
+              mail.setTimer(mail.INTERVAL_BATTERY_LOW);
               console.log('Low battery, checking the server less frequently.');
             }, false);
             
             window.addEventListener('batterycritical', function () {
-              clearTimeout(mail.timer);
-              mail.interval_current = null;
+              mail.setTimer(null);
               console.log('Critically low battery, stopped checking the server.');
             }, false);
           </script>

Received on Friday, 1 July 2011 07:40:14 UTC