diff --git a/.changeset/strange-baboons-protect.md b/.changeset/strange-baboons-protect.md new file mode 100644 index 00000000..6b64558a --- /dev/null +++ b/.changeset/strange-baboons-protect.md @@ -0,0 +1,5 @@ +--- +"@jspsych-contrib/extension-countdown": major +--- + +Added countdown extension. diff --git a/README.md b/README.md index 8d166a23..4a4a5a9b 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,7 @@ Plugin/Extension | Contributor | Description [audio-multi-response](https://github.com/jspsych/jspsych-contrib/blob/main/packages/plugin-audio-multi-response/README.md) | [Adam Richie-Halford](https://github.com/richford) | This plugin collects responses to an audio file using both button clicks and key presses. [audio-swipe-response](https://github.com/jspsych/jspsych-contrib/blob/main/packages/plugin-audio-swipe-response/README.md) | [Adam Richie-Halford](https://github.com/richford) | This plugin collects responses to an audio file using swipe gestures and keyboard responses. [corsi-blocks](https://github.com/jspsych/jspsych-contrib/blob/main/packages/plugin-corsi-blocks/README.md) | [Josh de Leeuw](https://github.com/jodeleeuw) | This plugin displays a configurable Corsi blocks task and records a series of click responses. +[countdown](https://github.com/jspsych/jspsych-contrib/blob/main/packages/extension-countdown/README.md) | [Shaobin Jiang](https://github.com/Shaobin-Jiang) | This extension adds a countdown during a trial. [gamepad](https://github.com/jspsych/jspsych-contrib/blob/main/packages/plugin-gamepad/README.md) | [Shaobin Jiang](https://github.com/Shaobin-Jiang) | This plugin allows one to use gamepads in a jsPsych experiment. [html-choice](https://github.com/jspsych/jspsych-contrib/blob/main/packages/plugin-html-choice/README.md) | [Younes Strittmatter](https://github.com/younesStrittmatter) | This plugin displays clickable html elements that can be used to present a choice. [html-multi-response](https://github.com/jspsych/jspsych-contrib/blob/main/packages/plugin-html-multi-response/README.md) | [Adam Richie-Halford](https://github.com/richford) | This plugin collects responses to an arbitrary HTML string using both button clicks and key presses. diff --git a/package-lock.json b/package-lock.json index 1db05357..1cdc7ef3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3095,6 +3095,10 @@ "@jridgewell/sourcemap-codec": "^1.4.14" } }, + "node_modules/@jspsych-contrib/extension-countdown": { + "resolved": "packages/extension-countdown", + "link": true + }, "node_modules/@jspsych-contrib/extension-device-motion": { "resolved": "packages/extension-device-motion", "link": true @@ -16536,6 +16540,18 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "packages/extension-countdown": { + "version": "1.0.0", + "license": "MIT", + "devDependencies": { + "@jspsych/config": "^2.0.0", + "@jspsych/test-utils": "^1.0.0", + "jspsych": "^7.0.0" + }, + "peerDependencies": { + "jspsych": ">=7.0.0" + } + }, "packages/extension-device-motion": { "name": "@jspsych-contrib/extension-device-motion", "version": "1.0.0", @@ -19132,6 +19148,14 @@ "@jridgewell/sourcemap-codec": "^1.4.14" } }, + "@jspsych-contrib/extension-countdown": { + "version": "file:packages/extension-countdown", + "requires": { + "@jspsych/config": "^2.0.0", + "@jspsych/test-utils": "^1.0.0", + "jspsych": "^7.0.0" + } + }, "@jspsych-contrib/extension-device-motion": { "version": "file:packages/extension-device-motion", "requires": { diff --git a/packages/extension-countdown/docs/jspsych-countdown.md b/packages/extension-countdown/docs/jspsych-countdown.md new file mode 100644 index 00000000..aa8a32ae --- /dev/null +++ b/packages/extension-countdown/docs/jspsych-countdown.md @@ -0,0 +1,95 @@ +# countdown + +This extension adds a countdown during a trial. + +## Parameters + +### Initialization Parameters + +None + +### Trial Parameters + +Trial parameters can be set when adding the extension to a trial object. + +```javascript +let trial = { + type: jsPsych..., + extensions: [ + {type: jsPsychExtensionWebgazer, params: {...}} + ] +} +``` + +| Parameter | Type | Default Value | Description | +| --------- | ---- | ------------- | ----------- | +| time | number | undefined | Time in milliseconds of the countdown | +| update_time | number | 50 | How often to update the countdown display; in milliseconds | +| format | function | (time) => String(Math.floor(time / 1000)) | The displayed content of the countdown. Receives the current time left in milliseconds and returns a string for display. | + +## Data Generated + +None + +## Functions + +These functions below are provided to enable a better interaction with the countdown. Note that all of the functions below must be prefixed with `jsPsych.extensions.countdown` (e.g. `jsPsych.extensions.countdown.pause()`). + +### `pause()` + +Pauses the countdown. + +### `resume()` + +Resumes the countdown. + +## Example + +```javascript +let jsPsych = initJsPsych({ + extensions: [{ type: jsPsychExtensionCountdown }], +}); + +let trial = { + type: jsPsychHtmlKeyboardResponse, + stimulus: "Hello world", + extensions: [ + { + type: jsPsychExtensionCountdown, + params: { + time: 5000, + update_time: 20, + format: (time) => { + if (time < 3000) { + document.querySelector(".jspsych-extension-countdown").style.color = "red"; + } + + let time_in_seconds = time / 1000; + + let minutes = Math.floor(time_in_seconds / 60); + time_in_seconds -= minutes * 60; + + let seconds = Math.floor(time_in_seconds); + + let format_number = (number) => { + let temp_str = `0${number}`; + return temp_str.substring(temp_str.length - 2); + }; + + return `${format_number(minutes)}:${format_number(seconds)}`; + }, + }, + }, + ], + on_load: function () { + setTimeout(() => { + jsPsych.extensions.countdown.pause(); + setTimeout(() => { + jsPsych.extensions.countdown.resume(); + }, 2000); + }, 1000); + }, +}; + +jsPsych.run([trial]); +``` diff --git a/packages/extension-countdown/examples/example.html b/packages/extension-countdown/examples/example.html new file mode 100644 index 00000000..8f6d3eca --- /dev/null +++ b/packages/extension-countdown/examples/example.html @@ -0,0 +1,64 @@ + + +
+ + +