W3C home > Mailing lists > Public > public-dap-commits@w3.org > July 2011

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

From: Anssi Kostiainen via cvs-syncmail <cvsmail@w3.org>
Date: Fri, 01 Jul 2011 07:40:12 +0000
To: public-dap-commits@w3.org
Message-Id: <E1QcYKi-0006t1-Ta@lionel-hutz.w3.org>
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 @@
           &lt;title&gt;Email Client&lt;/title&gt;
           &lt;script&gt;
             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);
           &lt;/script&gt;
         &lt;/head&gt;
@@ -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 &amp;&amp; (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 &amp;&amp; (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);
           &lt;/script&gt;
Received on Friday, 1 July 2011 07:40:14 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Friday, 1 July 2011 07:40:14 GMT