-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
67b811c
commit 0eba45c
Showing
1 changed file
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>Countdown Extension Example</title> | ||
<script src="https://unpkg.com/[email protected]"></script> | ||
<script src="https://unpkg.com/@jspsych/[email protected]"></script> | ||
<script src="../dist/index.browser.min.js"></script> | ||
<link href="https://unpkg.com/[email protected]/css/jspsych.css" rel="stylesheet" /> | ||
</head> | ||
<body> | ||
<script> | ||
let jsPsych = initJsPsych({ | ||
extensions: [{ type: jsPsychExtensionCountdown }], | ||
}); | ||
|
||
let trial = { | ||
type: jsPsychHtmlKeyboardResponse, | ||
stimulus: "Hello world", | ||
extensions: [ | ||
{ | ||
type: jsPsychExtensionCountdown, | ||
params: { | ||
time: 5000, | ||
update_time: 20, | ||
// Change the format of the countdown string | ||
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)}`; | ||
}, | ||
}, | ||
}, | ||
], | ||
// Pause / Resume midway | ||
on_load: function () { | ||
setTimeout(() => { | ||
jsPsych.extensions.countdown.pause(); | ||
setTimeout(() => { | ||
jsPsych.extensions.countdown.resume(); | ||
}, 2000); | ||
}, 1000); | ||
}, | ||
}; | ||
|
||
jsPsych.run([trial]); | ||
</script> | ||
</body> | ||
</html> |