1
0
mirror of https://github.com/psychopy/psychojs.git synced 2025-05-10 18:50:54 +00:00
psychojs/docs/util_Clock.js.html

314 lines
10 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: util/Clock.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: util/Clock.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>/**
* Clock component.
*
* @author Alain Pitiot
* @version 2021.2.0
* @copyright (c) 2017-2020 Ilixa Ltd. (http://ilixa.com) (c) 2020-2021 Open Science Tools Ltd. (https://opensciencetools.org)
* @license Distributed under the terms of the MIT License
*/
/**
* &lt;p>MonotonicClock offers a convenient way to keep track of time during experiments. An experiment can have as many independent clocks as needed, e.g. one to time responses, another one to keep track of stimuli, etc.&lt;/p>
*
* @name module:util.MonotonicClock
* @class
* @param {number} [startTime= &lt;time elapsed since the reference point, i.e. the time when the module was loaded>] - the clock's start time (in ms)
*/
export class MonotonicClock
{
constructor(startTime = MonotonicClock.getReferenceTime())
{
this._timeAtLastReset = startTime;
}
/**
* Get the current time on this clock.
*
* @name module:util.MonotonicClock#getTime
* @function
* @public
* @return {number} the current time (in seconds)
*/
getTime()
{
return MonotonicClock.getReferenceTime() - this._timeAtLastReset;
}
/**
* Get the current offset being applied to the high resolution timebase used by this Clock.
*
* @name module:util.MonotonicClock#getLastResetTime
* @function
* @public
* @return {number} the offset (in seconds)
*/
getLastResetTime()
{
return this._timeAtLastReset;
}
/**
* Get the time elapsed since the reference point.
*
* @name module:util.MonotonicClock#getReferenceTime
* @function
* @public
* @return {number} the time elapsed since the reference point (in seconds)
*/
static getReferenceTime()
{
return (performance.now() / 1000.0 - MonotonicClock._referenceTime);
// return (new Date().getTime()) / 1000.0 - MonotonicClock._referenceTime;
}
/**
* Get the current timestamp with language-sensitive formatting rules applied.
*
* &lt;p>Note: This is just a convenience wrapper around `Intl.DateTimeFormat()`.&lt;/p>
*
* @name module:util.MonotonicClock.getDate
* @function
* @public
* @static
* @param {string|array.string} locales - A string with a BCP 47 language tag, or an array of such strings.
* @param {object} options - An object with detailed date and time styling information.
* @return {string} The current timestamp in the chosen format.
*/
static getDate(locales = 'en-CA', optionsMaybe)
{
const date = new Date();
const options = Object.assign({
hour12: false,
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: 'numeric',
minute: 'numeric',
second: 'numeric',
fractionalSecondDigits: 3
}, optionsMaybe);
const dateTimeFormat = new Intl.DateTimeFormat(locales, options);
return dateTimeFormat.format(date);
}
/**
* Get the clock's current time in the default format filtering out file name unsafe characters.
*
* &lt;p>Note: This is mostly used as an appendix to the name of the keys save to the server.&lt;/p>
*
* @name module:util.MonotonicClock.getDateStr
* @function
* @public
* @static
* @return {string} A string representing the current time formatted as YYYY-MM-DD_HH[h]mm.ss.sss
*/
static getDateStr()
{
// yyyy-mm-dd, hh:mm:ss.sss
return MonotonicClock.getDate()
// yyyy-mm-dd_hh:mm:ss.sss
.replace(', ', '_')
// yyyy-mm-dd_hh[h]mm:ss.sss
.replace(':', 'h')
// yyyy-mm-dd_hh[h]mm.ss.sss
.replace(':', '.');
}
}
/**
* The clock's referenceTime is the time when the module was loaded (in seconds).
*
* @name module:util.MonotonicClock._referenceTime
* @readonly
* @private
* @type {number}
*/
MonotonicClock._referenceTime = performance.now() / 1000.0;
// MonotonicClock._referenceTime = new Date().getTime() / 1000.0;
/**
* &lt;p>Clock is a MonotonicClock that also offers the possibility of being reset.&lt;/p>
*
* @name module:util.Clock
* @class
* @extends MonotonicClock
*/
export class Clock extends MonotonicClock
{
constructor()
{
super();
}
/**
* Reset the time on the clock.
*
*
* @name module:util.Clock#reset
* @function
* @public
* @param {number} [newTime= 0] the new time on the clock.
*/
reset(newTime = 0)
{
this._timeAtLastReset = MonotonicClock.getReferenceTime() + newTime;
}
/**
* Add more time to the clock's 'start' time (t0).
*
* &lt;p>Note: by adding time to t0, the current time is pushed forward (it becomes
* smaller). As a consequence, getTime() may return a negative number.&lt;/p>
*
* @name module:util.Clock#add
* @function
* @public
* @param {number} [deltaTime] the time to be added to the clock's start time (t0)
*/
add(deltaTime)
{
this._timeAtLastReset += deltaTime;
}
}
/**
* &lt;p>CountdownTimer is a clock counts down from the time of last reset.&lt;/p.
*
* @name module:util.CountdownTimer
* @class
* @extends Clock
* @param {number} [startTime= 0] - the start time of the countdown
*/
export class CountdownTimer extends Clock
{
constructor(startTime = 0)
{
super();
this._timeAtLastReset = MonotonicClock.getReferenceTime();
this._countdown_duration = startTime;
if (startTime)
{
this.add(startTime);
}
}
/**
* Add more time to the clock's 'start' time (t0).
*
* &lt;p>Note: by adding time to t0, you push the current time forward (make it
* smaller). As a consequence, getTime() may return a negative number.&lt;/p>
*
* @name module:util.CountdownTimer#add
* @function
* @public
* @param {number} [deltaTime] the time to be added to the clock's start time (t0)
*/
add(deltaTime)
{
this._timeAtLastReset += deltaTime;
}
/**
* Reset the time on the countdown.
*
* @name module:util.CountdownTimer#reset
* @function
* @public
* @param {number} [newTime] - if newTime is undefined, the countdown time is reset to zero, otherwise we set it
* to newTime
*/
reset(newTime = undefined)
{
if (typeof newTime == 'undefined')
{
this._timeAtLastReset = MonotonicClock.getReferenceTime() + this._countdown_duration;
}
else
{
this._countdown_duration = newTime;
this._timeAtLastReset = MonotonicClock.getReferenceTime() + newTime;
}
}
/**
* Get the time currently left on the countdown.
*
* @name module:util.CountdownTimer#getTime
* @function
* @public
* @return {number} the time left on the countdown (in seconds)
*/
getTime()
{
return this._timeAtLastReset - MonotonicClock.getReferenceTime();
}
}
</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-core.html">core</a></li><li><a href="module-data.html">data</a></li><li><a href="module-sound.html">sound</a></li><li><a href="module-util.html">util</a></li><li><a href="module-visual.html">visual</a></li></ul><h3>Classes</h3><ul><li><a href="module-core.BuilderKeyResponse.html">BuilderKeyResponse</a></li><li><a href="module-core.EventManager.html">EventManager</a></li><li><a href="module-core.GUI.html">GUI</a></li><li><a href="module-core.Keyboard.html">Keyboard</a></li><li><a href="module-core.KeyPress.html">KeyPress</a></li><li><a href="module-core.Logger.html">Logger</a></li><li><a href="module-core.MinimalStim.html">MinimalStim</a></li><li><a href="module-core.Mouse.html">Mouse</a></li><li><a href="module-core.PsychoJS.html">PsychoJS</a></li><li><a href="module-core.ServerManager.html">ServerManager</a></li><li><a href="module-core.Window.html">Window</a></li><li><a href="module-data.ExperimentHandler.html">ExperimentHandler</a></li><li><a href="module-data.TrialHandler.html">TrialHandler</a></li><li><a href="module-sound.AudioClip.html">AudioClip</a></li><li><a href="module-sound.AudioClipPlayer.html">AudioClipPlayer</a></li><li><a href="module-sound.Microphone.html">Microphone</a></li><li><a href="module-sound.Sound.html">Sound</a></li><li><a href="module-sound.TonePlayer.html">TonePlayer</a></li><li><a href="module-sound.TrackPlayer.html">TrackPlayer</a></li><li><a href="module-util.Clock.html">Clock</a></li><li><a href="module-util.Color.html">Color</a></li><li><a href="module-util.CountdownTimer.html">CountdownTimer</a></li><li><a href="module-util.EventEmitter.html">EventEmitter</a></li><li><a href="module-util.MixinBuilder.html">MixinBuilder</a></li><li><a href="module-util.MonotonicClock.html">MonotonicClock</a></li><li><a href="module-util.PsychObject.html">PsychObject</a></li><li><a href="module-util.Scheduler.html">Scheduler</a></li><li><a href="module-visual.ButtonStim.html">ButtonStim</a></li><li><a href="module-visual.Form.html">Form</a></li><li><a href="module-visual.ImageStim.html">ImageStim</a></li><li><a href="module-visual.MovieStim.html">MovieStim</a></li><li><a href="module-visual.Polygon.html">Polygon</a></li><li><a href="module-visual.Rect.html">Rect</a></li><li><a href="module-visual.ShapeStim.html">ShapeStim</a></li><li><a href="module-visual.Slider.html">Slider</a></li><li><a href="module-visual.TextBox.html">TextBox</a></li><li><a href="module-visual.TextStim.html">TextStim</a></li><li><a href="module-visual.VisualStim.html">VisualStim</a></li></ul><h3>Interfaces</h3><ul><li><a href="module-sound.SoundPlayer.html">SoundPlayer</a></li></ul><h3>Mixins</h3><ul><li><a href="module-core.WindowMixin.html">WindowMixin</a></li><li><a href="module-util.ColorMixin.html">ColorMixin</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.7</a> on Mon Jun 21 2021 07:34:20 GMT+0200 (Central European Summer Time)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>