jspsych 6.2.0 → 7.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (360) hide show
  1. package/README.md +43 -29
  2. package/css/jspsych.css +39 -39
  3. package/dist/JsPsych.d.ts +112 -0
  4. package/dist/TimelineNode.d.ts +34 -0
  5. package/dist/index.browser.js +3164 -0
  6. package/dist/index.browser.js.map +1 -0
  7. package/dist/index.browser.min.js +2 -0
  8. package/dist/index.browser.min.js.map +1 -0
  9. package/dist/index.cjs +3158 -0
  10. package/dist/index.cjs.map +1 -0
  11. package/dist/index.d.ts +11 -0
  12. package/dist/index.js +3152 -0
  13. package/dist/index.js.map +1 -0
  14. package/dist/migration.d.ts +3 -0
  15. package/dist/modules/data/DataCollection.d.ts +45 -0
  16. package/dist/modules/data/DataColumn.d.ts +15 -0
  17. package/dist/modules/data/index.d.ts +25 -0
  18. package/dist/modules/data/utils.d.ts +3 -0
  19. package/dist/modules/extensions.d.ts +22 -0
  20. package/dist/modules/plugin-api/HardwareAPI.d.ts +15 -0
  21. package/dist/modules/plugin-api/KeyboardListenerAPI.d.ts +34 -0
  22. package/dist/modules/plugin-api/MediaAPI.d.ts +27 -0
  23. package/dist/modules/plugin-api/SimulationAPI.d.ts +41 -0
  24. package/dist/modules/plugin-api/TimeoutAPI.d.ts +5 -0
  25. package/dist/modules/plugin-api/index.d.ts +8 -0
  26. package/dist/modules/plugins.d.ts +129 -0
  27. package/dist/modules/randomization.d.ts +35 -0
  28. package/dist/modules/turk.d.ts +40 -0
  29. package/dist/modules/utils.d.ts +7 -0
  30. package/package.json +32 -15
  31. package/src/JsPsych.ts +884 -0
  32. package/src/TimelineNode.ts +536 -0
  33. package/src/index.ts +71 -0
  34. package/src/migration.ts +37 -0
  35. package/src/modules/data/DataCollection.ts +198 -0
  36. package/src/modules/data/DataColumn.ts +86 -0
  37. package/src/modules/data/index.ts +174 -0
  38. package/src/modules/data/utils.ts +75 -0
  39. package/src/modules/extensions.ts +23 -0
  40. package/src/modules/plugin-api/HardwareAPI.ts +32 -0
  41. package/src/modules/plugin-api/KeyboardListenerAPI.ts +165 -0
  42. package/src/modules/plugin-api/MediaAPI.ts +337 -0
  43. package/src/modules/plugin-api/SimulationAPI.ts +181 -0
  44. package/src/modules/plugin-api/TimeoutAPI.ts +16 -0
  45. package/src/modules/plugin-api/index.ts +28 -0
  46. package/src/modules/plugins.ts +158 -0
  47. package/src/modules/randomization.ts +327 -0
  48. package/src/modules/turk.ts +99 -0
  49. package/src/modules/utils.ts +30 -0
  50. package/.github/workflows/jest.yml +0 -20
  51. package/code-of-conduct.md +0 -56
  52. package/contributors.md +0 -61
  53. package/docs/CNAME +0 -1
  54. package/docs/about/about.md +0 -18
  55. package/docs/about/contributing.md +0 -43
  56. package/docs/about/license.md +0 -25
  57. package/docs/about/support.md +0 -7
  58. package/docs/core_library/jspsych-core.md +0 -661
  59. package/docs/core_library/jspsych-data.md +0 -589
  60. package/docs/core_library/jspsych-pluginAPI.md +0 -510
  61. package/docs/core_library/jspsych-randomization.md +0 -397
  62. package/docs/core_library/jspsych-turk.md +0 -102
  63. package/docs/img/blue.png +0 -0
  64. package/docs/img/folder-setup.png +0 -0
  65. package/docs/img/folder-with-html.png +0 -0
  66. package/docs/img/githubreleases.jpg +0 -0
  67. package/docs/img/jspsych-favicon.png +0 -0
  68. package/docs/img/jspsych-logo-no-text-mono.svg +0 -493
  69. package/docs/img/jspsych-logo.jpg +0 -0
  70. package/docs/img/orange.png +0 -0
  71. package/docs/img/palmer_stim.png +0 -0
  72. package/docs/img/progress_bar.png +0 -0
  73. package/docs/img/visual_search_example.jpg +0 -0
  74. package/docs/index.md +0 -9
  75. package/docs/overview/browser-device-support.md +0 -35
  76. package/docs/overview/callbacks.md +0 -140
  77. package/docs/overview/data.md +0 -281
  78. package/docs/overview/exclude-browser.md +0 -32
  79. package/docs/overview/experiment-options.md +0 -121
  80. package/docs/overview/fullscreen.md +0 -36
  81. package/docs/overview/media-preloading.md +0 -91
  82. package/docs/overview/mturk.md +0 -77
  83. package/docs/overview/progress-bar.md +0 -110
  84. package/docs/overview/record-browser-interactions.md +0 -23
  85. package/docs/overview/running-experiments.md +0 -95
  86. package/docs/overview/timeline.md +0 -387
  87. package/docs/overview/trial.md +0 -142
  88. package/docs/plugins/creating-a-plugin.md +0 -79
  89. package/docs/plugins/jspsych-animation.md +0 -40
  90. package/docs/plugins/jspsych-audio-button-response.md +0 -60
  91. package/docs/plugins/jspsych-audio-keyboard-response.md +0 -58
  92. package/docs/plugins/jspsych-audio-slider-response.md +0 -53
  93. package/docs/plugins/jspsych-call-function.md +0 -81
  94. package/docs/plugins/jspsych-canvas-button-response.md +0 -66
  95. package/docs/plugins/jspsych-canvas-keyboard-response.md +0 -68
  96. package/docs/plugins/jspsych-canvas-slider-response.md +0 -89
  97. package/docs/plugins/jspsych-categorize-animation.md +0 -60
  98. package/docs/plugins/jspsych-categorize-html.md +0 -52
  99. package/docs/plugins/jspsych-categorize-image.md +0 -53
  100. package/docs/plugins/jspsych-cloze.md +0 -45
  101. package/docs/plugins/jspsych-external-html.md +0 -70
  102. package/docs/plugins/jspsych-free-sort.md +0 -55
  103. package/docs/plugins/jspsych-fullscreen.md +0 -57
  104. package/docs/plugins/jspsych-html-button-response.md +0 -42
  105. package/docs/plugins/jspsych-html-keyboard-response.md +0 -51
  106. package/docs/plugins/jspsych-html-slider-response.md +0 -45
  107. package/docs/plugins/jspsych-iat-html.md +0 -64
  108. package/docs/plugins/jspsych-iat-image.md +0 -64
  109. package/docs/plugins/jspsych-image-button-response.md +0 -46
  110. package/docs/plugins/jspsych-image-keyboard-response.md +0 -57
  111. package/docs/plugins/jspsych-image-slider-response.md +0 -52
  112. package/docs/plugins/jspsych-instructions.md +0 -58
  113. package/docs/plugins/jspsych-maxdiff.md +0 -42
  114. package/docs/plugins/jspsych-rdk.md +0 -119
  115. package/docs/plugins/jspsych-reconstruction.md +0 -48
  116. package/docs/plugins/jspsych-resize.md +0 -39
  117. package/docs/plugins/jspsych-same-different-html.md +0 -53
  118. package/docs/plugins/jspsych-same-different-image.md +0 -66
  119. package/docs/plugins/jspsych-serial-reaction-time-mouse.md +0 -50
  120. package/docs/plugins/jspsych-serial-reaction-time.md +0 -57
  121. package/docs/plugins/jspsych-survey-html-form.md +0 -50
  122. package/docs/plugins/jspsych-survey-likert.md +0 -70
  123. package/docs/plugins/jspsych-survey-multi-choice.md +0 -48
  124. package/docs/plugins/jspsych-survey-multi-select.md +0 -53
  125. package/docs/plugins/jspsych-survey-text.md +0 -63
  126. package/docs/plugins/jspsych-video-button-response.md +0 -52
  127. package/docs/plugins/jspsych-video-keyboard-response.md +0 -48
  128. package/docs/plugins/jspsych-video-slider-response.md +0 -58
  129. package/docs/plugins/jspsych-visual-search-circle.md +0 -52
  130. package/docs/plugins/jspsych-vsl-animate-occlusion.md +0 -55
  131. package/docs/plugins/jspsych-vsl-grid-scene.md +0 -62
  132. package/docs/plugins/overview.md +0 -111
  133. package/docs/tutorials/hello-world.md +0 -144
  134. package/docs/tutorials/rt-task.md +0 -1107
  135. package/examples/add-to-end-of-timeline.html +0 -32
  136. package/examples/conditional-and-loop-functions.html +0 -63
  137. package/examples/css/jquery-ui.css +0 -1225
  138. package/examples/data-add-properties.html +0 -40
  139. package/examples/data-as-function.html +0 -36
  140. package/examples/data-from-timeline.html +0 -45
  141. package/examples/data-from-url.html +0 -21
  142. package/examples/demo-flanker.html +0 -108
  143. package/examples/demo-simple-rt-task.html +0 -104
  144. package/examples/demos/demo_1.html +0 -29
  145. package/examples/demos/demo_2.html +0 -43
  146. package/examples/demos/demo_3.html +0 -58
  147. package/examples/display-element-to-embed-experiment.html +0 -73
  148. package/examples/end-active-node.html +0 -52
  149. package/examples/end-experiment.html +0 -43
  150. package/examples/exclusions.html +0 -32
  151. package/examples/external_html/simple_consent.html +0 -4
  152. package/examples/img/1.gif +0 -0
  153. package/examples/img/10.gif +0 -0
  154. package/examples/img/11.gif +0 -0
  155. package/examples/img/12.gif +0 -0
  156. package/examples/img/2.gif +0 -0
  157. package/examples/img/3.gif +0 -0
  158. package/examples/img/4.gif +0 -0
  159. package/examples/img/5.gif +0 -0
  160. package/examples/img/6.gif +0 -0
  161. package/examples/img/7.gif +0 -0
  162. package/examples/img/8.gif +0 -0
  163. package/examples/img/9.gif +0 -0
  164. package/examples/img/age/of1.jpg +0 -0
  165. package/examples/img/age/of2.jpg +0 -0
  166. package/examples/img/age/of3.jpg +0 -0
  167. package/examples/img/age/om1.jpg +0 -0
  168. package/examples/img/age/om2.jpg +0 -0
  169. package/examples/img/age/om3.jpg +0 -0
  170. package/examples/img/age/yf1.jpg +0 -0
  171. package/examples/img/age/yf4.jpg +0 -0
  172. package/examples/img/age/yf5.jpg +0 -0
  173. package/examples/img/age/ym2.jpg +0 -0
  174. package/examples/img/age/ym3.jpg +0 -0
  175. package/examples/img/age/ym5.jpg +0 -0
  176. package/examples/img/backwardN.gif +0 -0
  177. package/examples/img/blue.png +0 -0
  178. package/examples/img/con1.png +0 -0
  179. package/examples/img/con2.png +0 -0
  180. package/examples/img/fixation.gif +0 -0
  181. package/examples/img/happy_face_1.jpg +0 -0
  182. package/examples/img/happy_face_2.jpg +0 -0
  183. package/examples/img/happy_face_3.jpg +0 -0
  184. package/examples/img/happy_face_4.jpg +0 -0
  185. package/examples/img/inc1.png +0 -0
  186. package/examples/img/inc2.png +0 -0
  187. package/examples/img/normalN.gif +0 -0
  188. package/examples/img/orange.png +0 -0
  189. package/examples/img/redX.png +0 -0
  190. package/examples/img/ribbon.jpg +0 -0
  191. package/examples/img/sad_face_1.jpg +0 -0
  192. package/examples/img/sad_face_2.jpg +0 -0
  193. package/examples/img/sad_face_3.jpg +0 -0
  194. package/examples/img/sad_face_4.jpg +0 -0
  195. package/examples/js/snap.svg-min.js +0 -21
  196. package/examples/jspsych-RDK.html +0 -58
  197. package/examples/jspsych-animation.html +0 -33
  198. package/examples/jspsych-audio-button-response.html +0 -52
  199. package/examples/jspsych-audio-keyboard-response.html +0 -62
  200. package/examples/jspsych-audio-slider-response.html +0 -55
  201. package/examples/jspsych-call-function.html +0 -32
  202. package/examples/jspsych-canvas-button-response.html +0 -95
  203. package/examples/jspsych-canvas-keyboard-response.html +0 -78
  204. package/examples/jspsych-canvas-slider-response.html +0 -67
  205. package/examples/jspsych-categorize-animation.html +0 -46
  206. package/examples/jspsych-categorize-html.html +0 -38
  207. package/examples/jspsych-categorize-image.html +0 -38
  208. package/examples/jspsych-cloze.html +0 -42
  209. package/examples/jspsych-free-sort.html +0 -97
  210. package/examples/jspsych-fullscreen.html +0 -44
  211. package/examples/jspsych-html-button-response.html +0 -46
  212. package/examples/jspsych-html-keyboard-response.html +0 -42
  213. package/examples/jspsych-html-slider-response.html +0 -53
  214. package/examples/jspsych-iat.html +0 -510
  215. package/examples/jspsych-image-button-response.html +0 -84
  216. package/examples/jspsych-image-keyboard-response.html +0 -78
  217. package/examples/jspsych-image-slider-response.html +0 -76
  218. package/examples/jspsych-instructions.html +0 -37
  219. package/examples/jspsych-maxdiff.html +0 -33
  220. package/examples/jspsych-reconstruction.html +0 -43
  221. package/examples/jspsych-resize.html +0 -34
  222. package/examples/jspsych-same-different-html.html +0 -28
  223. package/examples/jspsych-same-different-image.html +0 -33
  224. package/examples/jspsych-serial-reaction-time-mouse.html +0 -98
  225. package/examples/jspsych-serial-reaction-time.html +0 -54
  226. package/examples/jspsych-survey-html-form.html +0 -33
  227. package/examples/jspsych-survey-likert.html +0 -42
  228. package/examples/jspsych-survey-multi-choice.html +0 -40
  229. package/examples/jspsych-survey-multi-select.html +0 -42
  230. package/examples/jspsych-survey-text.html +0 -34
  231. package/examples/jspsych-video-button-response.html +0 -57
  232. package/examples/jspsych-video-keyboard-response.html +0 -53
  233. package/examples/jspsych-video-slider-response.html +0 -55
  234. package/examples/jspsych-visual-search-circle.html +0 -58
  235. package/examples/jspsych-vsl-animate-occlusion.html +0 -29
  236. package/examples/jspsych-vsl-grid-scene.html +0 -41
  237. package/examples/lexical-decision.html +0 -132
  238. package/examples/manual-preloading.html +0 -53
  239. package/examples/pause-unpause.html +0 -33
  240. package/examples/progress-bar.html +0 -62
  241. package/examples/sound/hammer.mp3 +0 -0
  242. package/examples/sound/sound.mp3 +0 -0
  243. package/examples/sound/speech_blue.mp3 +0 -0
  244. package/examples/sound/speech_green.mp3 +0 -0
  245. package/examples/sound/speech_joke.mp3 +0 -0
  246. package/examples/sound/speech_red.mp3 +0 -0
  247. package/examples/sound/tone.mp3 +0 -0
  248. package/examples/timeline-variables-sampling.html +0 -50
  249. package/examples/timeline-variables.html +0 -55
  250. package/examples/video/sample_video.mp4 +0 -0
  251. package/jspsych.js +0 -2796
  252. package/license.txt +0 -21
  253. package/mkdocs.yml +0 -104
  254. package/plugins/jspsych-animation.js +0 -189
  255. package/plugins/jspsych-audio-button-response.js +0 -247
  256. package/plugins/jspsych-audio-keyboard-response.js +0 -204
  257. package/plugins/jspsych-audio-slider-response.js +0 -262
  258. package/plugins/jspsych-call-function.js +0 -58
  259. package/plugins/jspsych-canvas-button-response.js +0 -199
  260. package/plugins/jspsych-canvas-keyboard-response.js +0 -155
  261. package/plugins/jspsych-canvas-slider-response.js +0 -207
  262. package/plugins/jspsych-categorize-animation.js +0 -266
  263. package/plugins/jspsych-categorize-html.js +0 -220
  264. package/plugins/jspsych-categorize-image.js +0 -222
  265. package/plugins/jspsych-cloze.js +0 -112
  266. package/plugins/jspsych-external-html.js +0 -112
  267. package/plugins/jspsych-free-sort.js +0 -444
  268. package/plugins/jspsych-fullscreen.js +0 -104
  269. package/plugins/jspsych-html-button-response.js +0 -188
  270. package/plugins/jspsych-html-keyboard-response.js +0 -149
  271. package/plugins/jspsych-html-slider-response.js +0 -202
  272. package/plugins/jspsych-iat-html.js +0 -284
  273. package/plugins/jspsych-iat-image.js +0 -286
  274. package/plugins/jspsych-image-button-response.js +0 -311
  275. package/plugins/jspsych-image-keyboard-response.js +0 -247
  276. package/plugins/jspsych-image-slider-response.js +0 -353
  277. package/plugins/jspsych-instructions.js +0 -237
  278. package/plugins/jspsych-maxdiff.js +0 -174
  279. package/plugins/jspsych-rdk.js +0 -1373
  280. package/plugins/jspsych-reconstruction.js +0 -134
  281. package/plugins/jspsych-resize.js +0 -166
  282. package/plugins/jspsych-same-different-html.js +0 -168
  283. package/plugins/jspsych-same-different-image.js +0 -169
  284. package/plugins/jspsych-serial-reaction-time-mouse.js +0 -213
  285. package/plugins/jspsych-serial-reaction-time.js +0 -247
  286. package/plugins/jspsych-survey-html-form.js +0 -171
  287. package/plugins/jspsych-survey-likert.js +0 -195
  288. package/plugins/jspsych-survey-multi-choice.js +0 -208
  289. package/plugins/jspsych-survey-multi-select.js +0 -232
  290. package/plugins/jspsych-survey-text.js +0 -185
  291. package/plugins/jspsych-video-button-response.js +0 -320
  292. package/plugins/jspsych-video-keyboard-response.js +0 -279
  293. package/plugins/jspsych-video-slider-response.js +0 -351
  294. package/plugins/jspsych-visual-search-circle.js +0 -259
  295. package/plugins/jspsych-vsl-animate-occlusion.js +0 -196
  296. package/plugins/jspsych-vsl-grid-scene.js +0 -103
  297. package/plugins/template/jspsych-plugin-template.js +0 -35
  298. package/tests/README.md +0 -7
  299. package/tests/jsPsych/default-iti.test.js +0 -51
  300. package/tests/jsPsych/default-parameters.test.js +0 -58
  301. package/tests/jsPsych/endexperiment.test.js +0 -49
  302. package/tests/jsPsych/events.test.js +0 -369
  303. package/tests/jsPsych/init.test.js +0 -48
  304. package/tests/jsPsych/loads.test.js +0 -7
  305. package/tests/jsPsych/min-rt.test.js +0 -58
  306. package/tests/jsPsych/progressbar.test.js +0 -202
  307. package/tests/jsPsych/timeline-variables.test.js +0 -254
  308. package/tests/jsPsych/timelines.test.js +0 -498
  309. package/tests/jsPsych.data/datacollection.test.js +0 -116
  310. package/tests/jsPsych.data/datacolumn.test.js +0 -50
  311. package/tests/jsPsych.data/datamodule.test.js +0 -152
  312. package/tests/jsPsych.data/dataparameter.test.js +0 -251
  313. package/tests/jsPsych.data/interactions.test.js +0 -109
  314. package/tests/jsPsych.pluginAPI/pluginapi.test.js +0 -144
  315. package/tests/jsPsych.randomization/randomziation.test.js +0 -27
  316. package/tests/jsPsych.utils/utils.test.js +0 -58
  317. package/tests/media/blue.png +0 -0
  318. package/tests/media/orange.png +0 -0
  319. package/tests/media/sample_video.mp4 +0 -0
  320. package/tests/media/sound.mp3 +0 -0
  321. package/tests/plugins/plugin-animation.test.js +0 -35
  322. package/tests/plugins/plugin-audio-button-response.test.js +0 -15
  323. package/tests/plugins/plugin-audio-keyboard-response.test.js +0 -15
  324. package/tests/plugins/plugin-audio-slider-response.test.js +0 -15
  325. package/tests/plugins/plugin-call-function.test.js +0 -49
  326. package/tests/plugins/plugin-categorize-animation.test.js +0 -274
  327. package/tests/plugins/plugin-categorize-html.test.js +0 -17
  328. package/tests/plugins/plugin-categorize-image.test.js +0 -17
  329. package/tests/plugins/plugin-cloze.test.js +0 -140
  330. package/tests/plugins/plugin-free-sort.test.js +0 -112
  331. package/tests/plugins/plugin-fullscreen.test.js +0 -41
  332. package/tests/plugins/plugin-html-button-response.test.js +0 -161
  333. package/tests/plugins/plugin-html-keyboard-response.test.js +0 -139
  334. package/tests/plugins/plugin-html-slider-response.test.js +0 -155
  335. package/tests/plugins/plugin-iat-html.test.js +0 -328
  336. package/tests/plugins/plugin-iat-image.test.js +0 -308
  337. package/tests/plugins/plugin-image-button-response.test.js +0 -183
  338. package/tests/plugins/plugin-image-keyboard-response.test.js +0 -154
  339. package/tests/plugins/plugin-image-slider-response.test.js +0 -183
  340. package/tests/plugins/plugin-instructions.test.js +0 -66
  341. package/tests/plugins/plugin-maxdiff.test.js +0 -39
  342. package/tests/plugins/plugin-rdk.test.js +0 -17
  343. package/tests/plugins/plugin-reconstruction.test.js +0 -16
  344. package/tests/plugins/plugin-resize.test.js +0 -16
  345. package/tests/plugins/plugin-same-different-html.test.js +0 -17
  346. package/tests/plugins/plugin-same-different-image.test.js +0 -17
  347. package/tests/plugins/plugin-serial-reaction-time-mouse.test.js +0 -42
  348. package/tests/plugins/plugin-serial-reaction-time.test.js +0 -69
  349. package/tests/plugins/plugin-survey-html-form.test.js +0 -44
  350. package/tests/plugins/plugin-survey-likert.test.js +0 -48
  351. package/tests/plugins/plugin-survey-multi-choice.test.js +0 -48
  352. package/tests/plugins/plugin-survey-multi-select.test.js +0 -72
  353. package/tests/plugins/plugin-survey-text.test.js +0 -115
  354. package/tests/plugins/plugin-video-button-response.test.js +0 -35
  355. package/tests/plugins/plugin-video-keyboard-response.test.js +0 -35
  356. package/tests/plugins/plugin-video-slider-response.test.js +0 -34
  357. package/tests/plugins/plugin-visual-search-circle.test.js +0 -16
  358. package/tests/plugins/plugin-vsl-animate-occlusion.test.js +0 -16
  359. package/tests/plugins/plugin-vsl-grid-scene.test.js +0 -16
  360. package/tests/testing-utils.js +0 -13
@@ -1,1107 +0,0 @@
1
- # Summary of Tutorial Content
2
-
3
- This tutorial will work through the creation of a simple response time task. The task is to press one key for a blue colored circle and another key for an orange colored circle. Despite this simple task, the tutorial covers many of the key features of jsPsych, including:
4
-
5
- * Using a plugin to create a standard trial.
6
- * Combining plugins together to create new kinds of trials.
7
- * Using timeline variables to maximize code reuse.
8
- * Randomizing presentation order.
9
- * Manipulating, filtering, and aggregating data.
10
- * Using dynamic content to change the experiment parameters based on the subject's responses.
11
-
12
- ## Part 1: Creating a blank experiment
13
-
14
- Start by downloading jsPsych and setting up a folder to contain your experiment files. If you are unsure how to do this, follow steps 1-5 in the [Hello World tutorial](hello-world.md). At the end of step 5 in the Hello World tutorial, you should have an experiment page that looks like this:
15
-
16
- ```html
17
- <!DOCTYPE html>
18
- <html>
19
- <head>
20
- <title>My experiment</title>
21
- <script src="jspsych-6.0.5/jspsych.js"></script>
22
- <script src="jspsych-6.0.5/plugins/jspsych-html-keyboard-response.js"></script>
23
- <link href="jspsych-6.0.5/css/jspsych.css" rel="stylesheet" type="text/css">
24
- </head>
25
- <body></body>
26
- </html>
27
- ```
28
-
29
- This will be our starting point for building the rest of the experiment.
30
-
31
- ## Part 2: Display welcome message
32
-
33
- All jsPsych experiments are defined by a timeline. The timeline is an array that contains the set of trials we want to run in the experiment. We can start by defining the timeline array.
34
-
35
- ```javascript
36
- var timeline = [];
37
- ```
38
-
39
- Let's greet the subject with a simple welcome message using the [jspsych-html-keyboard-response](../plugins/jspsych-html-keyboard-response.md) plugin.
40
-
41
- First, we create a trial that uses the jspsych-html-keyboard-response plugin and contains a simple string to show the subject.
42
-
43
- ```javascript
44
- var welcome = {
45
- type: "html-keyboard-response",
46
- stimulus: "Welcome to the experiment. Press any key to begin."
47
- };
48
- ```
49
-
50
- Next, we push the welcome trial to the timeline, which adds it to the end of the array.
51
-
52
- ```javascript
53
- timeline.push(welcome);
54
- ```
55
-
56
- Finally, we tell jsPsych to run the experiment by calling the [jsPsych.init() function](../core_library/jspsych-core.md#jspsychinit) and passing in the array that defines the experiment timeline.
57
-
58
- ```javascript
59
- jsPsych.init({
60
- timeline: timeline
61
- });
62
- ```
63
-
64
- ### The complete code so far
65
-
66
- ```html
67
- <!DOCTYPE html>
68
- <html>
69
- <head>
70
- <title>My experiment</title>
71
- <script src="jspsych-6.0.5/jspsych.js"></script>
72
- <script src="jspsych-6.0.5/plugins/jspsych-html-keyboard-response.js"></script>
73
- <link href="jspsych-6.0.5/css/jspsych.css" rel="stylesheet" type="text/css">
74
- </head>
75
- <body></body>
76
- <script>
77
-
78
- /* create timeline */
79
- var timeline = [];
80
-
81
- /* define welcome message trial */
82
- var welcome = {
83
- type: "html-keyboard-response",
84
- stimulus: "Welcome to the experiment. Press any key to begin."
85
- };
86
- timeline.push(welcome);
87
-
88
- /* start the experiment */
89
- jsPsych.init({
90
- timeline: timeline
91
- });
92
- </script>
93
- </html>
94
- ```
95
-
96
- ## Part 3: Show instructions
97
-
98
- We can use the same basic structure from part 2 to create a new trial that shows instructions to the subject. The only difference in this trial is that we will use HTML formatting to control how the instructions display.
99
-
100
- The trial definition looks like this:
101
-
102
- ```javascript
103
- var instructions = {
104
- type: "html-keyboard-response",
105
- stimulus: "<p>In this experiment, a circle will appear in the center " +
106
- "of the screen.</p><p>If the circle is <strong>blue</strong>, " +
107
- "press the letter F on the keyboard as fast as you can.</p>" +
108
- "<p>If the circle is <strong>orange</strong>, press the letter J " +
109
- "as fast as you can.</p>" +
110
- "<div style='width: 700px;'>"+
111
- "<div style='float: left;'><img src='img/blue.png'></img>" +
112
- "<p class='small'><strong>Press the F key</strong></p></div>" +
113
- "<div class='float: right;'><img src='img/orange.png'></img>" +
114
- "<p class='small'><strong>Press the J key</strong></p></div>" +
115
- "</div>"+
116
- "<p>Press any key to begin.</p>"
117
- };
118
- ```
119
-
120
- Notice that the HTML includes `<img>` tags to display the images that the subject will be responding to. You'll need to download these image files. Right-click on each image below and select *Save image as...*. Put the images in a folder called `img` in the experiment folder you created in part 1.
121
-
122
- ![blue circle](../img/blue.png)
123
- ![orange circle](../img/orange.png)
124
-
125
- Don't forget to add the trial to the timeline:
126
-
127
- ```javascript
128
- timeline.push(instructions);
129
- ```
130
-
131
- ### The complete code so far
132
-
133
- ```html
134
- <!DOCTYPE html>
135
- <html>
136
- <head>
137
- <title>My experiment</title>
138
- <script src="jspsych-6.0.5/jspsych.js"></script>
139
- <script src="jspsych-6.0.5/plugins/jspsych-html-keyboard-response.js"></script>
140
- <link href="jspsych-6.0.5/css/jspsych.css" rel="stylesheet" type="text/css">
141
- </head>
142
- <body></body>
143
- <script>
144
-
145
- /* create timeline */
146
- var timeline = [];
147
-
148
- /* define welcome message trial */
149
- var welcome = {
150
- type: "html-keyboard-response",
151
- stimulus: "Welcome to the experiment. Press any key to begin."
152
- };
153
- timeline.push(welcome);
154
-
155
- /* define instructions trial */
156
- var instructions = {
157
- type: "html-keyboard-response",
158
- stimulus: "<p>In this experiment, a circle will appear in the center " +
159
- "of the screen.</p><p>If the circle is <strong>blue</strong>, " +
160
- "press the letter F on the keyboard as fast as you can.</p>" +
161
- "<p>If the circle is <strong>orange</strong>, press the letter J " +
162
- "as fast as you can.</p>" +
163
- "<div style='width: 700px;'>"+
164
- "<div style='float: left;'><img src='img/blue.png'></img>" +
165
- "<p class='small'><strong>Press the F key</strong></p></div>" +
166
- "<div class='float: right;'><img src='img/orange.png'></img>" +
167
- "<p class='small'><strong>Press the J key</strong></p></div>" +
168
- "</div>"+
169
- "<p>Press any key to begin.</p>",
170
- post_trial_gap: 2000
171
- };
172
- timeline.push(instructions);
173
-
174
- /* start the experiment */
175
- jsPsych.init({
176
- timeline: timeline
177
- });
178
- </script>
179
- </html>
180
- ```
181
-
182
- ## Part 4: Displaying stimuli and getting responses
183
-
184
- Creating trials to show the stimuli is conceptually the same as creating a trial to show instructions, except that now we are displaying an image instead of text or html. This means we need to use a different plugin: jspsych-image-keyboard-response. We need to start by loading this plugin by adding a `<script>` tag to the document.
185
-
186
- ```html
187
- <head>
188
- <title>My experiment</title>
189
- <script src="jspsych-6.0.5/jspsych.js"></script>
190
- <script src="jspsych-6.0.5/plugins/jspsych-html-keyboard-response.js"></script>
191
- <script src="jspsych-6.0.5/plugins/jspsych-image-keyboard-response.js"></script>
192
- <link href="jspsych-6.0.5/css/jspsych.css" rel="stylesheet" type="text/css">
193
- </head>
194
- ```
195
-
196
- For now, we will just show each image once. The path to the image file should be set as the `stimulus` parameter. We will also set the option for which keys the subject is allowed to use to respond (`choices`) so that only the 'f' and 'j' keys are valid responses.
197
-
198
- ```javascript
199
- var blue_trial = {
200
- type: 'image-keyboard-response',
201
- stimulus: 'img/blue.png',
202
- choices: ['f', 'j']
203
- };
204
-
205
- var orange_trial = {
206
- type: 'image-keyboard-response',
207
- stimulus: 'img/orange.png',
208
- choices: ['f', 'j']
209
- }
210
- ```
211
-
212
- As usual, we need to add the trials to the timeline.
213
-
214
- ```javascript
215
- timeline.push(blue_trial, orange_trial);
216
- ```
217
-
218
- ### The complete code so far
219
-
220
- ```html
221
- <!DOCTYPE html>
222
- <html>
223
- <head>
224
- <title>My experiment</title>
225
- <script src="jspsych-6.0.5/jspsych.js"></script>
226
- <script src="jspsych-6.0.5/plugins/jspsych-html-keyboard-response.js"></script>
227
- <script src="jspsych-6.0.5/plugins/jspsych-image-keyboard-response.js"></script>
228
- <link href="jspsych-6.0.5/css/jspsych.css" rel="stylesheet" type="text/css">
229
- </head>
230
- <body></body>
231
- <script>
232
-
233
- /* create timeline */
234
- var timeline = [];
235
-
236
- /* define welcome message trial */
237
- var welcome = {
238
- type: "html-keyboard-response",
239
- stimulus: "Welcome to the experiment. Press any key to begin."
240
- };
241
- timeline.push(welcome);
242
-
243
- /* define instructions trial */
244
- var instructions = {
245
- type: "html-keyboard-response",
246
- stimulus: "<p>In this experiment, a circle will appear in the center " +
247
- "of the screen.</p><p>If the circle is <strong>blue</strong>, " +
248
- "press the letter F on the keyboard as fast as you can.</p>" +
249
- "<p>If the circle is <strong>orange</strong>, press the letter J " +
250
- "as fast as you can.</p>" +
251
- "<div style='width: 700px;'>"+
252
- "<div style='float: left;'><img src='img/blue.png'></img>" +
253
- "<p class='small'><strong>Press the F key</strong></p></div>" +
254
- "<div class='float: right;'><img src='img/orange.png'></img>" +
255
- "<p class='small'><strong>Press the J key</strong></p></div>" +
256
- "</div>"+
257
- "<p>Press any key to begin.</p>",
258
- post_trial_gap: 2000
259
- };
260
- timeline.push(instructions);
261
-
262
- /* test trials */
263
- var blue_trial = {
264
- type: 'image-keyboard-response',
265
- stimulus: 'img/blue.png',
266
- choices: ['f', 'j']
267
- };
268
-
269
- var orange_trial = {
270
- type: 'image-keyboard-response',
271
- stimulus: 'img/orange.png',
272
- choices: ['f', 'j']
273
- }
274
-
275
- timeline.push(blue_trial, orange_trial);
276
-
277
- /* start the experiment */
278
- jsPsych.init({
279
- timeline: timeline
280
- });
281
- </script>
282
- </html>
283
- ```
284
-
285
-
286
- ## Part 5: Timeline variables
287
-
288
- In the full experiment, we will want more than two trials. One way we could do this is to create many more objects that define trials and push them all onto the timeline, but there is a more efficient way: using timeline variables.
289
-
290
- The parameters for showing the blue and orange circle are very similar. The only difference is which image is displayed. Timeline variables allow us to define the procedure for showing the stimulus once, and then repeatedly use it with different variables. We'll see how, even in this relatively simple case, this can save us a lot of lines of code.
291
-
292
- To start, let's make an array that contains all the different trials we want to run in the test phase. There are only two for the experiment: blue trials and orange trials.
293
-
294
- ```javascript
295
- var test_stimuli = [
296
- { stimulus: "img/blue.png"},
297
- { stimulus: "img/orange.png"}
298
- ];
299
- ```
300
-
301
- Instead of just showing the blue and orange circles, let's also set up the experiment to show a fixation cross (+) in between trials. We can define a trial to show the fixation cross for a fixed amount of time by using the `trial_duration` parameter of the html-keyboard-response plugin and setting the `choices` parameter to the special value `jsPsych.NO_KEYS`, which means that no responses will be accepted as a valid response and the trial will last however long the `trial_duration` parameter specifies.
302
-
303
- ```javascript
304
- var fixation = {
305
- type: 'html-keyboard-response',
306
- stimulus: '<div style="font-size:60px;">+</div>',
307
- choices: jsPsych.NO_KEYS,
308
- trial_duration: 1000,
309
- }
310
- ```
311
-
312
- To show the circles, we'll set up another trial with the html-keyboard-response plugin, but we'll use the function `jsPsych.timelineVariable()` to indicate that we want jsPsych to substitute the value of the parameter in from the timeline variables.
313
-
314
- ```javascript
315
- var test = {
316
- type: "image-keyboard-response",
317
- stimulus: jsPsych.timelineVariable('stimulus'),
318
- choices: ['f', 'j']
319
- }
320
- ```
321
-
322
- To link the variables that we declared in the `test_stimuli` array with the call to `jsPsych.timelineVariable()` we need to create a new timeline and set the `timeline_variables` property:
323
-
324
- ```javascript
325
- var test_procedure = {
326
- timeline: [fixation, test],
327
- timeline_variables: test_stimuli
328
- }
329
- ```
330
-
331
- We have to add the `test_procedure` to the main `timeline` array, but the fixation and test trial do not need to be added to `timeline` because they already exist on the `test_procedure` timeline.
332
-
333
- ```javascript
334
- timeline.push(test_procedure);
335
- ```
336
-
337
- What happens when the experiment reaches the test procedure? jsPsych will run the `test_procedure` timeline one time for each entry in the `test_stimuli` array (twice, in this case). The first time through, jsPsych will substitute the timeline variables from the first array entry (blue image), and the second time through the second array entry will be used (orange image). Notice that the fixation trial occurs before both the orange and the blue circles, because the entire timeline of the `test_procedure` is repeated for each entry in the `timeline_variables` array.
338
-
339
- ### The complete code so far
340
-
341
- ```html
342
- <!DOCTYPE html>
343
- <html>
344
- <head>
345
- <title>My experiment</title>
346
- <script src="jspsych-6.0.5/jspsych.js"></script>
347
- <script src="jspsych-6.0.5/plugins/jspsych-html-keyboard-response.js"></script>
348
- <script src="jspsych-6.0.5/plugins/jspsych-image-keyboard-response.js"></script>
349
- <link href="jspsych-6.0.5/css/jspsych.css" rel="stylesheet" type="text/css">
350
- </head>
351
- <body></body>
352
- <script>
353
-
354
- /* create timeline */
355
- var timeline = [];
356
-
357
- /* define welcome message trial */
358
- var welcome = {
359
- type: "html-keyboard-response",
360
- stimulus: "Welcome to the experiment. Press any key to begin."
361
- };
362
- timeline.push(welcome);
363
-
364
- /* define instructions trial */
365
- var instructions = {
366
- type: "html-keyboard-response",
367
- stimulus: "<p>In this experiment, a circle will appear in the center " +
368
- "of the screen.</p><p>If the circle is <strong>blue</strong>, " +
369
- "press the letter F on the keyboard as fast as you can.</p>" +
370
- "<p>If the circle is <strong>orange</strong>, press the letter J " +
371
- "as fast as you can.</p>" +
372
- "<div style='width: 700px;'>"+
373
- "<div style='float: left;'><img src='img/blue.png'></img>" +
374
- "<p class='small'><strong>Press the F key</strong></p></div>" +
375
- "<div class='float: right;'><img src='img/orange.png'></img>" +
376
- "<p class='small'><strong>Press the J key</strong></p></div>" +
377
- "</div>"+
378
- "<p>Press any key to begin.</p>",
379
- post_trial_gap: 2000
380
- };
381
- timeline.push(instructions);
382
-
383
- /* test trials */
384
- var test_stimuli = [
385
- { stimulus: "img/blue.png"},
386
- { stimulus: "img/orange.png"}
387
- ];
388
-
389
- var fixation = {
390
- type: 'html-keyboard-response',
391
- stimulus: '<div style="font-size:60px;">+</div>',
392
- choices: jsPsych.NO_KEYS,
393
- trial_duration: 1000,
394
- }
395
-
396
- var test = {
397
- type: "image-keyboard-response",
398
- stimulus: jsPsych.timelineVariable('stimulus'),
399
- choices: ['f', 'j']
400
- }
401
-
402
- var test_procedure = {
403
- timeline: [fixation, test],
404
- timeline_variables: test_stimuli
405
- }
406
-
407
- timeline.push(test_procedure);
408
-
409
- /* start the experiment */
410
- jsPsych.init({
411
- timeline: timeline
412
- });
413
- </script>
414
- </html>
415
- ```
416
-
417
-
418
- ## Part 6: Parameters for timelines with timeline variables
419
-
420
- Right now our experiment is a measly two trials long. Even worse is that the order of the stimuli is the same every time! When we use timeline variables, we get access to some very easy-to-use methods to randomize the order and repeat the trials. To randomize the order, simply set `randomize_order: true` on the object with the `timeline_variables`:
421
-
422
- ```javascript
423
- var test_procedure = {
424
- timeline: [fixation, test],
425
- timeline_variables: test_stimuli,
426
- randomize_order: true
427
- }
428
- ```
429
-
430
- We can also easily make the test phase longer by setting the `repetitions` parameter. This parameter controls how many times the experiment will loop through all of the entries in the timeline_variables array. For example, if we set `repetitions: 5`, then the experiment will loop through the two entries in the timeline_variables 5 times, for a total of 10 test trials.
431
-
432
- ```javascript
433
- var test_procedure = {
434
- timeline: [fixation, test],
435
- timeline_variables: test_stimuli,
436
- randomize_order: true,
437
- repetitions: 5
438
- }
439
- ```
440
- ### The complete code so far
441
-
442
- ```html
443
- <!DOCTYPE html>
444
- <html>
445
- <head>
446
- <title>My experiment</title>
447
- <script src="jspsych-6.0.5/jspsych.js"></script>
448
- <script src="jspsych-6.0.5/plugins/jspsych-html-keyboard-response.js"></script>
449
- <script src="jspsych-6.0.5/plugins/jspsych-image-keyboard-response.js"></script>
450
- <link href="jspsych-6.0.5/css/jspsych.css" rel="stylesheet" type="text/css">
451
- </head>
452
- <body></body>
453
- <script>
454
-
455
- /* create timeline */
456
- var timeline = [];
457
-
458
- /* define welcome message trial */
459
- var welcome = {
460
- type: "html-keyboard-response",
461
- stimulus: "Welcome to the experiment. Press any key to begin."
462
- };
463
- timeline.push(welcome);
464
-
465
- /* define instructions trial */
466
- var instructions = {
467
- type: "html-keyboard-response",
468
- stimulus: "<p>In this experiment, a circle will appear in the center " +
469
- "of the screen.</p><p>If the circle is <strong>blue</strong>, " +
470
- "press the letter F on the keyboard as fast as you can.</p>" +
471
- "<p>If the circle is <strong>orange</strong>, press the letter J " +
472
- "as fast as you can.</p>" +
473
- "<div style='width: 700px;'>"+
474
- "<div style='float: left;'><img src='img/blue.png'></img>" +
475
- "<p class='small'><strong>Press the F key</strong></p></div>" +
476
- "<div class='float: right;'><img src='img/orange.png'></img>" +
477
- "<p class='small'><strong>Press the J key</strong></p></div>" +
478
- "</div>"+
479
- "<p>Press any key to begin.</p>",
480
- post_trial_gap: 2000
481
- };
482
- timeline.push(instructions);
483
-
484
- /* test trials */
485
- var test_stimuli = [
486
- { stimulus: "img/blue.png"},
487
- { stimulus: "img/orange.png"}
488
- ];
489
-
490
- var fixation = {
491
- type: 'html-keyboard-response',
492
- stimulus: '<div style="font-size:60px;">+</div>',
493
- choices: jsPsych.NO_KEYS,
494
- trial_duration: 1000,
495
- }
496
-
497
- var test = {
498
- type: "image-keyboard-response",
499
- stimulus: jsPsych.timelineVariable('stimulus'),
500
- choices: ['f', 'j']
501
- }
502
-
503
- var test_procedure = {
504
- timeline: [fixation, test],
505
- timeline_variables: test_stimuli,
506
- randomize_order: true,
507
- repetitions: 5
508
- }
509
-
510
- timeline.push(test_procedure);
511
-
512
- /* start the experiment */
513
- jsPsych.init({
514
- timeline: timeline
515
- });
516
- </script>
517
- </html>
518
- ```
519
-
520
- ## Part 7: Using functions to generate parameters
521
-
522
- One aspect of the experiment that could be improved is the duration of the fixation cross. As the experiment stands right now, the timing of the circles appearing is very predictable. We can change that by using a different value for the `trial_duration` parameter in the `fixation` trial for each trial. But how can we do that and keep the simple code structure we have now where we only have to define the fixation trial once? One option would be to add another timeline variable, like `"fixation_duration"` and use that to control the timing. But another option is to specify the `trial_duration` parameter as a function. If a parameter is a function, jsPsych will execute the function every time the trial runs. That means that if the function returns different results probabilistically, we can get a different parameter value every time the trial runs.
523
-
524
- To do that here, we'll use one of the built-in randomization methods in [jsPsych's randomization module](../core_library/jspsych-randomization.md). `jsPsych.randomization.sampleWithoutReplacement()` takes an array of items to sample from and generates a new array of length *N* by sampling without replacement.
525
-
526
- ```javascript
527
- var fixation = {
528
- type: 'html-keyboard-response',
529
- stimulus: '<div style="font-size:60px;">+</div>',
530
- choices: jsPsych.NO_KEYS,
531
- trial_duration: function(){
532
- return jsPsych.randomization.sampleWithoutReplacement([250, 500, 750, 1000, 1250, 1500, 1750, 2000], 1)[0];
533
- }
534
- }
535
- ```
536
-
537
- In the code above, we replaced the `trial_duration: 1000` parameter in `fixation` with a function. Inside the function, we take a sample from the array `[250, 500, 750, 1000, 1250, 1500, 1750, 2000]` of size 1 (second parameter to `jsPsych.randomization.sampleWithoutReplacement`). The return value from calling `jsPsych.randomization.sampleWithoutReplacement` is an array of length 1, so we add the `[0]` selection at the end to get the value out of the array.
538
-
539
- ### The complete code so far
540
-
541
- ```html
542
- <!DOCTYPE html>
543
- <html>
544
- <head>
545
- <title>My experiment</title>
546
- <script src="jspsych-6.0.5/jspsych.js"></script>
547
- <script src="jspsych-6.0.5/plugins/jspsych-html-keyboard-response.js"></script>
548
- <script src="jspsych-6.0.5/plugins/jspsych-image-keyboard-response.js"></script>
549
- <link href="jspsych-6.0.5/css/jspsych.css" rel="stylesheet" type="text/css">
550
- </head>
551
- <body></body>
552
- <script>
553
-
554
- /* create timeline */
555
- var timeline = [];
556
-
557
- /* define welcome message trial */
558
- var welcome = {
559
- type: "html-keyboard-response",
560
- stimulus: "Welcome to the experiment. Press any key to begin."
561
- };
562
- timeline.push(welcome);
563
-
564
- /* define instructions trial */
565
- var instructions = {
566
- type: "html-keyboard-response",
567
- stimulus: "<p>In this experiment, a circle will appear in the center " +
568
- "of the screen.</p><p>If the circle is <strong>blue</strong>, " +
569
- "press the letter F on the keyboard as fast as you can.</p>" +
570
- "<p>If the circle is <strong>orange</strong>, press the letter J " +
571
- "as fast as you can.</p>" +
572
- "<div style='width: 700px;'>"+
573
- "<div style='float: left;'><img src='img/blue.png'></img>" +
574
- "<p class='small'><strong>Press the F key</strong></p></div>" +
575
- "<div class='float: right;'><img src='img/orange.png'></img>" +
576
- "<p class='small'><strong>Press the J key</strong></p></div>" +
577
- "</div>"+
578
- "<p>Press any key to begin.</p>",
579
- post_trial_gap: 2000
580
- };
581
- timeline.push(instructions);
582
-
583
- /* test trials */
584
- var test_stimuli = [
585
- { stimulus: "img/blue.png"},
586
- { stimulus: "img/orange.png"}
587
- ];
588
-
589
- var fixation = {
590
- type: 'html-keyboard-response',
591
- stimulus: '<div style="font-size:60px;">+</div>',
592
- choices: jsPsych.NO_KEYS,
593
- trial_duration: function(){
594
- return jsPsych.randomization.sampleWithoutReplacement([250, 500, 750, 1000, 1250, 1500, 1750, 2000], 1)[0];
595
- }
596
- }
597
-
598
- var test = {
599
- type: "image-keyboard-response",
600
- stimulus: jsPsych.timelineVariable('stimulus'),
601
- choices: ['f', 'j']
602
- }
603
-
604
- var test_procedure = {
605
- timeline: [fixation, test],
606
- timeline_variables: test_stimuli,
607
- randomize_order: true,
608
- repetitions: 5
609
- }
610
-
611
- timeline.push(test_procedure);
612
-
613
- /* start the experiment */
614
- jsPsych.init({
615
- timeline: timeline
616
- });
617
- </script>
618
- </html>
619
- ```
620
-
621
- ## Part 8: Displaying the data
622
-
623
- We have created a complete, if simple, experiment at this point, so let's take a look at the data being generated. jsPsych has a built-in [function called `jsPsych.data.displayData()`](../core_library/jspsych-data.md#jspsychdatadisplaydata) that is useful for debugging your experiment. It will remove all of the information on the screen and replace it with the raw data collected so far. This isn't terribly useful when you are actually running an experiment, but it's nice for checking the data during development.
624
-
625
- We need the `displayData` function to execute when the experiment ends. One way to do this is to use the [`on_finish` callback function](../overview/callbacks.md#on_finish-experiment). This function will automatically execute once all the trials in the experiment are finished. We can specify a function to call in the `init` method.
626
-
627
- ```javascript
628
- jsPsych.init({
629
- timeline: timeline,
630
- on_finish: function() {
631
- jsPsych.data.displayData();
632
- }
633
- });
634
- ```
635
-
636
- ### The complete code so far
637
-
638
- ```html
639
- <!DOCTYPE html>
640
- <html>
641
- <head>
642
- <title>My experiment</title>
643
- <script src="jspsych-6.0.5/jspsych.js"></script>
644
- <script src="jspsych-6.0.5/plugins/jspsych-html-keyboard-response.js"></script>
645
- <script src="jspsych-6.0.5/plugins/jspsych-image-keyboard-response.js"></script>
646
- <link href="jspsych-6.0.5/css/jspsych.css" rel="stylesheet" type="text/css">
647
- </head>
648
- <body></body>
649
- <script>
650
-
651
- /* create timeline */
652
- var timeline = [];
653
-
654
- /* define welcome message trial */
655
- var welcome = {
656
- type: "html-keyboard-response",
657
- stimulus: "Welcome to the experiment. Press any key to begin."
658
- };
659
- timeline.push(welcome);
660
-
661
- /* define instructions trial */
662
- var instructions = {
663
- type: "html-keyboard-response",
664
- stimulus: "<p>In this experiment, a circle will appear in the center " +
665
- "of the screen.</p><p>If the circle is <strong>blue</strong>, " +
666
- "press the letter F on the keyboard as fast as you can.</p>" +
667
- "<p>If the circle is <strong>orange</strong>, press the letter J " +
668
- "as fast as you can.</p>" +
669
- "<div style='width: 700px;'>"+
670
- "<div style='float: left;'><img src='img/blue.png'></img>" +
671
- "<p class='small'><strong>Press the F key</strong></p></div>" +
672
- "<div class='float: right;'><img src='img/orange.png'></img>" +
673
- "<p class='small'><strong>Press the J key</strong></p></div>" +
674
- "</div>"+
675
- "<p>Press any key to begin.</p>",
676
- post_trial_gap: 2000
677
- };
678
- timeline.push(instructions);
679
-
680
- /* test trials */
681
- var test_stimuli = [
682
- { stimulus: "img/blue.png"},
683
- { stimulus: "img/orange.png"}
684
- ];
685
-
686
- var fixation = {
687
- type: 'html-keyboard-response',
688
- stimulus: '<div style="font-size:60px;">+</div>',
689
- choices: jsPsych.NO_KEYS,
690
- trial_duration: function(){
691
- return jsPsych.randomization.sampleWithoutReplacement([250, 500, 750, 1000, 1250, 1500, 1750, 2000], 1)[0];
692
- }
693
- }
694
-
695
- var test = {
696
- type: "image-keyboard-response",
697
- stimulus: jsPsych.timelineVariable('stimulus'),
698
- choices: ['f', 'j']
699
- }
700
-
701
- var test_procedure = {
702
- timeline: [fixation, test],
703
- timeline_variables: test_stimuli,
704
- randomize_order: true,
705
- repetitions: 5
706
- }
707
-
708
- timeline.push(test_procedure);
709
-
710
- /* start the experiment */
711
- jsPsych.init({
712
- timeline: timeline,
713
- on_finish: function() {
714
- jsPsych.data.displayData();
715
- }
716
- });
717
- </script>
718
- </html>
719
- ```
720
-
721
- ## Part 9: Tagging trials with additional data
722
-
723
- All trials in jsPsych can be tagged with additional arbitrary data. This data will get stored alongside the data that the plugin normally generates, which allows experimenters to record properties of a trial along with the data from the trial.
724
-
725
- When might you use this feature? In this experiment, it would be nice to tag each trial with a circle as a test trial, so that the resulting data can be easily filtered to look at only the test trials. We also could tag the test trials with a property that indicates what the correct response should be (F for the blue circles, J for the orange).
726
-
727
- In our current code, we are using the timeline variables feature of jsPsych to choose which circle gets presented on a trial. Since we want to tag the trials differently based on which circle is presented, we need to add the tagging data to the `test_stimuli` array, and then use the `jsPsych.timelineVariable()` function to get the values and assign them to the `data` property of the trial.
728
-
729
- We start by modifying `test_stimuli`:
730
-
731
- ```javascript
732
- var test_stimuli = [
733
- { stimulus: "img/blue.png", data: {test_part: 'test', correct_response: 'f'}},
734
- { stimulus: "img/orange.png", data: {test_part: 'test', correct_response: 'j'}}
735
- ];
736
- ```
737
- Now we assign these values to the `data` parameter of the `test` trial.
738
-
739
- ```javascript
740
- var test = {
741
- type: "image-keyboard-response",
742
- stimulus: jsPsych.timelineVariable('stimulus'),
743
- choices: ['f', 'j'],
744
- data: jsPsych.timelineVariable('data')
745
- }
746
- ```
747
-
748
- Another kind of tagging that would be useful is to mark each fixation trial as such, to make removing the data from fixation trials easier. This is a simpler task, as we don't need to use the timeline variables feature. We can just add a `data` property to the `fixation` trial:
749
-
750
- ```js
751
- var fixation = {
752
- type: 'html-keyboard-response',
753
- stimulus: '<div style="font-size:60px;">+</div>',
754
- choices: jsPsych.NO_KEYS,
755
- trial_duration: function(){
756
- return jsPsych.randomization.sampleWithoutReplacement([250, 500, 750, 1000, 1250, 1500, 1750, 2000], 1)[0];
757
- },
758
- data: {test_part: 'fixation'}
759
- }
760
- ```
761
-
762
- ### The complete code so far
763
-
764
- ```html
765
- <!DOCTYPE html>
766
- <html>
767
- <head>
768
- <title>My experiment</title>
769
- <script src="jspsych-6.0.5/jspsych.js"></script>
770
- <script src="jspsych-6.0.5/plugins/jspsych-html-keyboard-response.js"></script>
771
- <script src="jspsych-6.0.5/plugins/jspsych-image-keyboard-response.js"></script>
772
- <link href="jspsych-6.0.5/css/jspsych.css" rel="stylesheet" type="text/css">
773
- </head>
774
- <body></body>
775
- <script>
776
-
777
- /* create timeline */
778
- var timeline = [];
779
-
780
- /* define welcome message trial */
781
- var welcome = {
782
- type: "html-keyboard-response",
783
- stimulus: "Welcome to the experiment. Press any key to begin."
784
- };
785
- timeline.push(welcome);
786
-
787
- /* define instructions trial */
788
- var instructions = {
789
- type: "html-keyboard-response",
790
- stimulus: "<p>In this experiment, a circle will appear in the center " +
791
- "of the screen.</p><p>If the circle is <strong>blue</strong>, " +
792
- "press the letter F on the keyboard as fast as you can.</p>" +
793
- "<p>If the circle is <strong>orange</strong>, press the letter J " +
794
- "as fast as you can.</p>" +
795
- "<div style='width: 700px;'>"+
796
- "<div style='float: left;'><img src='img/blue.png'></img>" +
797
- "<p class='small'><strong>Press the F key</strong></p></div>" +
798
- "<div class='float: right;'><img src='img/orange.png'></img>" +
799
- "<p class='small'><strong>Press the J key</strong></p></div>" +
800
- "</div>"+
801
- "<p>Press any key to begin.</p>",
802
- post_trial_gap: 2000
803
- };
804
- timeline.push(instructions);
805
-
806
- /* test trials */
807
- var test_stimuli = [
808
- { stimulus: "img/blue.png", data: {test_part: 'test', correct_response: 'f'}},
809
- { stimulus: "img/orange.png", data: {test_part: 'test', correct_response: 'j'}}
810
- ];
811
-
812
- var fixation = {
813
- type: 'html-keyboard-response',
814
- stimulus: '<div style="font-size:60px;">+</div>',
815
- choices: jsPsych.NO_KEYS,
816
- trial_duration: function(){
817
- return jsPsych.randomization.sampleWithoutReplacement([250, 500, 750, 1000, 1250, 1500, 1750, 2000], 1)[0];
818
- },
819
- data: {test_part: 'fixation'}
820
- }
821
-
822
- var test = {
823
- type: "image-keyboard-response",
824
- stimulus: jsPsych.timelineVariable('stimulus'),
825
- choices: ['f', 'j'],
826
- data: jsPsych.timelineVariable('data')
827
- }
828
-
829
- var test_procedure = {
830
- timeline: [fixation, test],
831
- timeline_variables: test_stimuli,
832
- randomize_order: true,
833
- repetitions: 5
834
- }
835
-
836
- timeline.push(test_procedure);
837
-
838
- /* start the experiment */
839
- jsPsych.init({
840
- timeline: timeline,
841
- on_finish: function() {
842
- jsPsych.data.displayData();
843
- }
844
- });
845
- </script>
846
- </html>
847
- ```
848
-
849
- ## Part 10: Manipulating data during the experiment
850
-
851
- Now that the data from the test trials has a tag that describes the correct response, it would be easy to analyze the data after the fact (in R, for example) and calculate whether the participant responded correctly.
852
-
853
- But, we can also do this in jsPsych as the experiment runs to save time later and enable a limited set of data aggregation and analysis directly in the experiment code.
854
-
855
- To do this, we'll use the `on_finish` event of the test trial. We can assign a function to `on_finish`, and that function will receive an object containing the data generated by the trial. This object can be manipulated inside the function, and any changes made to the object will be stored in jsPsych's internal representation of the data.
856
-
857
- For this example, we'll calculate whether the subject responded correctly, and add a new `correct` property to the data object.
858
-
859
- ```javascript
860
- var test = {
861
- type: "image-keyboard-response",
862
- stimulus: jsPsych.timelineVariable('stimulus'),
863
- choices: ['f', 'j'],
864
- data: jsPsych.timelineVariable('data'),
865
- on_finish: function(data){
866
- data.correct = data.key_press == jsPsych.pluginAPI.convertKeyCharacterToKeyCode(data.correct_response);
867
- }
868
- }
869
- ```
870
-
871
- The `data.key_press` value is a numeric key code indicating which key the subject pressed. The function `jsPsych.pluginAPI.convertKeyCharacterToKeyCode` converts the character representation of a key into the numeric representation (e.g., calling the function on the value `'f'` generates the value `70`). If this numeric value matches `data.key_press` then `data.correct` will be `true`. Otherwise, it will be `false`.
872
-
873
- ### The complete code so far
874
-
875
- ```html
876
- <!DOCTYPE html>
877
- <html>
878
- <head>
879
- <title>My experiment</title>
880
- <script src="jspsych-6.0.5/jspsych.js"></script>
881
- <script src="jspsych-6.0.5/plugins/jspsych-html-keyboard-response.js"></script>
882
- <script src="jspsych-6.0.5/plugins/jspsych-image-keyboard-response.js"></script>
883
- <link href="jspsych-6.0.5/css/jspsych.css" rel="stylesheet" type="text/css">
884
- </head>
885
- <body></body>
886
- <script>
887
-
888
- /* create timeline */
889
- var timeline = [];
890
-
891
- /* define welcome message trial */
892
- var welcome = {
893
- type: "html-keyboard-response",
894
- stimulus: "Welcome to the experiment. Press any key to begin."
895
- };
896
- timeline.push(welcome);
897
-
898
- /* define instructions trial */
899
- var instructions = {
900
- type: "html-keyboard-response",
901
- stimulus: "<p>In this experiment, a circle will appear in the center " +
902
- "of the screen.</p><p>If the circle is <strong>blue</strong>, " +
903
- "press the letter F on the keyboard as fast as you can.</p>" +
904
- "<p>If the circle is <strong>orange</strong>, press the letter J " +
905
- "as fast as you can.</p>" +
906
- "<div style='width: 700px;'>"+
907
- "<div style='float: left;'><img src='img/blue.png'></img>" +
908
- "<p class='small'><strong>Press the F key</strong></p></div>" +
909
- "<div class='float: right;'><img src='img/orange.png'></img>" +
910
- "<p class='small'><strong>Press the J key</strong></p></div>" +
911
- "</div>"+
912
- "<p>Press any key to begin.</p>",
913
- post_trial_gap: 2000
914
- };
915
- timeline.push(instructions);
916
-
917
- /* test trials */
918
- var test_stimuli = [
919
- { stimulus: "img/blue.png", data: {test_part: 'test', correct_response: 'f'}},
920
- { stimulus: "img/orange.png", data: {test_part: 'test', correct_response: 'j'}}
921
- ];
922
-
923
- var fixation = {
924
- type: 'html-keyboard-response',
925
- stimulus: '<div style="font-size:60px;">+</div>',
926
- choices: jsPsych.NO_KEYS,
927
- trial_duration: function(){
928
- return jsPsych.randomization.sampleWithoutReplacement([250, 500, 750, 1000, 1250, 1500, 1750, 2000], 1)[0];
929
- },
930
- data: {test_part: 'fixation'}
931
- }
932
-
933
- var test = {
934
- type: "image-keyboard-response",
935
- stimulus: jsPsych.timelineVariable('stimulus'),
936
- choices: ['f', 'j'],
937
- data: jsPsych.timelineVariable('data'),
938
- on_finish: function(data){
939
- data.correct = data.key_press == jsPsych.pluginAPI.convertKeyCharacterToKeyCode(data.correct_response);
940
- }
941
- }
942
-
943
- var test_procedure = {
944
- timeline: [fixation, test],
945
- timeline_variables: test_stimuli,
946
- randomize_order: true,
947
- repetitions: 5
948
- }
949
-
950
- timeline.push(test_procedure);
951
-
952
- /* start the experiment */
953
- jsPsych.init({
954
- timeline: timeline,
955
- on_finish: function() {
956
- jsPsych.data.displayData();
957
- }
958
- });
959
- </script>
960
- </html>
961
- ```
962
-
963
-
964
- ## Part 11: Data aggregation
965
-
966
- A new feature in jsPsych version 6.0 is a suite of data aggregation functions. You can now easily calculate things like mean response times for a selected set of trials. In this part, we'll use these functions to add a final trial to the experiment that tells the subject their accuracy and their mean response time for correct responses.
967
-
968
- We'll use the text plugin. Because the actual text that we want to display changes based on the subject's performance in the experiment, we need to use a function for the `text` parameter and return the desired text.
969
-
970
- ```js
971
- var debrief_block = {
972
- type: "html-keyboard-response",
973
- stimulus: function() {
974
-
975
- var trials = jsPsych.data.get().filter({test_part: 'test'});
976
- var correct_trials = trials.filter({correct: true});
977
- var accuracy = Math.round(correct_trials.count() / trials.count() * 100);
978
- var rt = Math.round(correct_trials.select('rt').mean());
979
-
980
- return "<p>You responded correctly on "+accuracy+"% of the trials.</p>"+
981
- "<p>Your average response time was "+rt+"ms.</p>"+
982
- "<p>Press any key to complete the experiment. Thank you!</p>";
983
-
984
- }
985
- };
986
-
987
- timeline.push(debrief_block);
988
- ```
989
-
990
- To create the variable `trials`, we use `jsPsych.data.get()` which returns a jsPsych data collection containing all of the data from the experiment. We can then use `.filter` to select only the trials where `test_part` is `'test'` (a benefit of tagging the trials in part 9). `trials` contains all of the data from the trials where a circle was shown.
991
-
992
- To get only the correct trials, we can use `.filter()` again to select only the trials from the `trials` data collection where the property `correct` is `true`.
993
-
994
- To calculate accuracy, we can use the `.count()` method to determine how many trials were correct and how many trials there were total. We also use `Math.round()` to avoid extra digits after the decimal.
995
-
996
- Finally, to calculate the mean response time on correct trials, we use the `.select` method on the `correct_trials` data collection to select only the `'rt'` property of those trials. We can then use the `.mean()` method to find the mean of all the RT values.
997
-
998
- ## The final code
999
-
1000
- This code is available in the examples folder in the jsPsych download. It is called `demo-simple-rt-task.html`.
1001
-
1002
- ```html
1003
- <!DOCTYPE html>
1004
- <html>
1005
- <head>
1006
- <title>My experiment</title>
1007
- <script src="jspsych-6.0.5/jspsych.js"></script>
1008
- <script src="jspsych-6.0.5/plugins/jspsych-html-keyboard-response.js"></script>
1009
- <script src="jspsych-6.0.5/plugins/jspsych-image-keyboard-response.js"></script>
1010
- <link href="jspsych-6.0.5/css/jspsych.css" rel="stylesheet" type="text/css">
1011
- </head>
1012
- <body></body>
1013
- <script>
1014
-
1015
- /* create timeline */
1016
- var timeline = [];
1017
-
1018
- /* define welcome message trial */
1019
- var welcome = {
1020
- type: "html-keyboard-response",
1021
- stimulus: "Welcome to the experiment. Press any key to begin."
1022
- };
1023
- timeline.push(welcome);
1024
-
1025
- /* define instructions trial */
1026
- var instructions = {
1027
- type: "html-keyboard-response",
1028
- stimulus: "<p>In this experiment, a circle will appear in the center " +
1029
- "of the screen.</p><p>If the circle is <strong>blue</strong>, " +
1030
- "press the letter F on the keyboard as fast as you can.</p>" +
1031
- "<p>If the circle is <strong>orange</strong>, press the letter J " +
1032
- "as fast as you can.</p>" +
1033
- "<div style='width: 700px;'>"+
1034
- "<div style='float: left;'><img src='img/blue.png'></img>" +
1035
- "<p class='small'><strong>Press the F key</strong></p></div>" +
1036
- "<div class='float: right;'><img src='img/orange.png'></img>" +
1037
- "<p class='small'><strong>Press the J key</strong></p></div>" +
1038
- "</div>"+
1039
- "<p>Press any key to begin.</p>",
1040
- post_trial_gap: 2000
1041
- };
1042
- timeline.push(instructions);
1043
-
1044
- /* test trials */
1045
-
1046
- var test_stimuli = [
1047
- { stimulus: "img/blue.png", data: { test_part: 'test', correct_response: 'f' } },
1048
- { stimulus: "img/orange.png", data: { test_part: 'test', correct_response: 'j' } }
1049
- ];
1050
-
1051
- var fixation = {
1052
- type: 'html-keyboard-response',
1053
- stimulus: '<div style="font-size:60px;">+</div>',
1054
- choices: jsPsych.NO_KEYS,
1055
- trial_duration: function(){
1056
- return jsPsych.randomization.sampleWithoutReplacement([250, 500, 750, 1000, 1250, 1500, 1750, 2000], 1)[0];
1057
- },
1058
- data: {test_part: 'fixation'}
1059
- }
1060
-
1061
- var test = {
1062
- type: "image-keyboard-response",
1063
- stimulus: jsPsych.timelineVariable('stimulus'),
1064
- choices: ['f', 'j'],
1065
- data: jsPsych.timelineVariable('data'),
1066
- on_finish: function(data){
1067
- data.correct = data.key_press == jsPsych.pluginAPI.convertKeyCharacterToKeyCode(data.correct_response);
1068
- },
1069
- }
1070
-
1071
- var test_procedure = {
1072
- timeline: [fixation, test],
1073
- timeline_variables: test_stimuli,
1074
- repetitions: 5,
1075
- randomize_order: true
1076
- }
1077
- timeline.push(test_procedure);
1078
-
1079
- /* define debrief */
1080
-
1081
- var debrief_block = {
1082
- type: "html-keyboard-response",
1083
- stimulus: function() {
1084
-
1085
- var trials = jsPsych.data.get().filter({test_part: 'test'});
1086
- var correct_trials = trials.filter({correct: true});
1087
- var accuracy = Math.round(correct_trials.count() / trials.count() * 100);
1088
- var rt = Math.round(correct_trials.select('rt').mean());
1089
-
1090
- return "<p>You responded correctly on "+accuracy+"% of the trials.</p>"+
1091
- "<p>Your average response time was "+rt+"ms.</p>"+
1092
- "<p>Press any key to complete the experiment. Thank you!</p>";
1093
-
1094
- }
1095
- };
1096
- timeline.push(debrief_block);
1097
-
1098
- /* start the experiment */
1099
- jsPsych.init({
1100
- timeline: timeline,
1101
- on_finish: function() {
1102
- jsPsych.data.displayData();
1103
- }
1104
- });
1105
- </script>
1106
- </html>
1107
- ```