This is just a chronometer constructor in JavaScript, that uses ObjectEventTarget as prototype to support events listeners.
I want to create a demo how to use the ObjectEventTarget and inspire other JavaScript programmers to use prototype.
Nothing better then a project that evolves a logical part that can be totally separated from the design, and also have it owns events. This will allow you do a lot of things, so you also can prototype your function with the chronometer, creating a more sophisticate object.
You just need to create a instance or more using new Chronometer()
, then you are good to go.
npm install chronometer.js
It works fine in Nodejs and with Browserify, you can do a var Chronometer = require('chronometer.js');
and you are ready to go.
Public methods:
elapsedTime
, you can call start()
to continue tracking the timeelapsedTime
from global and current step.Internal use methods:
forceTime
is undefined, it will use Date.now()
as value of pausedTime
updateInterval
value.** Read-write properties:**
update
will be triggered by the autoUpdate
.** Read-only properties:
ChronometerStep
instances for each time you pause or step.Internal use properties:
Events are provided by ObjectEventTarget prototype, means that you can use addEventListener, removeEventListener or dispatchEvent in any instance of chronometer.
Cancelable Events:
Non-cancelable Events:
event
contains a detail
property with the old state value).To prototype you can call from your new constructor passing your context.
Example:
function FancyChronometer( name ){
Chronometer.call(this);
this.name = name;
}
FancyChronometer.prototype = Chronometer.prototype;
There is no private methods, except by the instance ChronometerStep
that only can be accessed by createStep
. The thing is, you have freedom to do whatever you want, even destroy the chronometer.
Object.defineProperty
isn't been used to protect the read only properties, because I want to be backward compatible with ES3 and because it's more about good sense to don't change it values, but if you need for any misterious reason, you can change it.
autoUpdateID
is an common example of how prototypes mean to be used if you want to give abbility to prototype any new constructor. If you want to hide it value from the other programmers, the only way is to use a WeakMap for that, and keep the methods that depends on it in the prototype, or create the method autoUpdate
with a closure inside the constructor, but it's more code, memory mess just to protect other programmers from doing mistake with it. Doesn't worth.
If your browser has support to Object.defineProperties
, it will add all methods as not enumerable, if you do a for in, they will not be iterated. But if you plan to give support to IE-9, use hasOwnProperty or a shim to ensure that behaviour.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4