Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #12111

Re: How to write a browser based timer

Path csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eweka.nl!lightspeed.eweka.nl!npeer.de.kpn-eurorings.net!npeer-ng0.de.kpn-eurorings.net!newsfeed.arcor.de!newsspool4.arcor-online.net!news.arcor.de.POSTED!not-for-mail
Content-Type text/plain; charset="UTF-8"
Message-ID <1938225.aqSPkdTlGX@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Reply-To Thomas 'PointedEars' Lahn <cljs@PointedEars.de>
Organization PointedEars Software (PES)
Date Thu, 23 Feb 2012 22:13:05 +0100
User-Agent KNode/4.4.11
Content-Transfer-Encoding 8Bit
Subject Re: How to write a browser based timer
Newsgroups comp.lang.javascript
References <ji101v$3ra$1@dont-email.me> <a44b4295-4c38-40c3-bc86-f63bd24d7720@b23g2000yqn.googlegroups.com> <ji5lko$7uj$1@dont-email.me>
Followup-To comp.lang.javascript
MIME-Version 1.0
Lines 55
NNTP-Posting-Date 23 Feb 2012 22:13:09 CET
NNTP-Posting-Host be3faa2e.newsspool2.arcor-online.net
X-Trace DXC=;^ZYG9RD8lA6PJ?[X6JIXEA9EHlD;3YcB4Fo<]lROoRA8kF<OcfhCOK:lDn3Y]T[_EDZm8W4\YJNLb@mF9jNikdEoj7e_2SK;JFiMJ1P`HV1gE
X-Complaints-To usenet-abuse@arcor.de
Xref x330-a1.tempe.blueboxinc.net comp.lang.javascript:12111

Followups directed to: comp.lang.javascript

Show key headers only | View raw


Cal Who wrote:

> "Scott Sauyet" <scott.sauyet@gmail.com> wrote […]:
>> You can certainly do what you want with Javascript.  And it shouldn't
>> be difficult.
>>
>> You need to look at three features of Javascript:
>>
>>    - Keyboard Events, to listen for the keystrokes that matter
>>    - setTimeout or setInterval to occasionally check the time
>>    - DOM manipulation to display the results

JFTR, the rather simple and proper, because reliable, approach is to call 
clearTimeout() if a timeout was set before, then setTimeout(), on keyup.  
Checking for the time that has passed since the last keypress is _not_ 
reliable by comparison.

  var timeout = null;
  
  function clearMe (timeout)
  {
    if (timeout !== null)
    {
      window.clearTimeout(timeout);
    }
  } 

  _addEventListener(control, "keyup", function () {
    clearMe(timeout);

    timeout = window.setTimeout(function () {
      /* do something */

      clearMe(timeout);
    }, 42);
  }, false);

where _addEventListener() is a wrapper for control.addEventListener() with 
proprietary event-handler properties as fallback, like 
jsx.dom.addEventListener() in JSX:event.js [1].  For a more object-oriented 
implementation of the approach mentioned before, one might want to look into 
JSX:timeout.js [2], in particular jsx.dom.timeout.Timeout.prototype.run().


PointedEars
___________
[1] 
<http://PointedEars.de/websvn/filedetails.php?repname=JSX&path=%2Ftrunk%2Fdom%2Fevents.js>
[2] 
<http://PointedEars.de/websvn/filedetails.php?repname=JSX&path=%2Ftrunk%2Fdom%2Ftimeout.js>
-- 
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

How to write a browser based timer " Cal Who" <CalWhoNOSPAM@roadrunner.com> - 2012-02-21 15:50 -0500
  Re: How to write a browser based timer Scott Sauyet <scott.sauyet@gmail.com> - 2012-02-21 13:04 -0800
    Re: How to write a browser based timer Gene Wirchenko <genew@ocis.net> - 2012-02-21 13:45 -0800
      Re: How to write a browser based timer "Evertjan." <exjxw.hannivoort@interxnl.net> - 2012-02-21 22:40 +0000
        Re: How to write a browser based timer Gene Wirchenko <genew@ocis.net> - 2012-02-21 15:04 -0800
          Re: How to write a browser based timer "Evertjan." <exjxw.hannivoort@interxnl.net> - 2012-02-21 23:27 +0000
            Re: How to write a browser based timer Gene Wirchenko <genew@ocis.net> - 2012-02-21 16:08 -0800
              Re: How to write a browser based timer "Evertjan." <exjxw.hannivoort@interxnl.net> - 2012-02-22 15:41 +0000
                Re: How to write a browser based timer Gene Wirchenko <genew@ocis.net> - 2012-02-22 09:19 -0800
                Re: How to write a browser based timer "Evertjan." <exjxw.hannivoort@interxnl.net> - 2012-02-22 21:55 +0000
                Re: How to write a browser based timer Gene Wirchenko <genew@ocis.net> - 2012-02-22 15:01 -0800
        Re: How to write a browser based timer RobG <rgqld@iinet.net.au> - 2012-02-21 18:18 -0800
          Re: How to write a browser based timer Dr J R Stockton <reply1208@merlyn.demon.co.uk> - 2012-02-23 19:31 +0000
            Re: How to write a browser based timer Gene Wirchenko <genew@ocis.net> - 2012-02-23 16:38 -0800
              Re: How to write a browser based timer Dr J R Stockton <reply1208@merlyn.demon.co.uk> - 2012-02-25 20:29 +0000
                Re: How to write a browser based timer "Evertjan." <exjxw.hannivoort@interxnl.net> - 2012-02-26 23:02 +0000
      Re: How to write a browser based timer Dr J R Stockton <jrs@merlyn.demon.co.uk> - 2012-02-22 18:51 +0000
    Re: How to write a browser based timer " Cal Who" <CalWhoNOSPAM@roadrunner.com> - 2012-02-23 10:23 -0500
      Re: How to write a browser based timer Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-02-23 22:13 +0100
        Re: How to write a browser based timer Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-02-24 09:41 +0100

csiph-web